首先,一些代码:
这是页面导航到时调用的初始操作:
public ActionResult ListCodeTypes()
{
var CodeList = _entities.Master_Codes.ToList();
List<SelectListItem> items = new List<SelectListItem>();
for (int i = 0; i < CodeList.Count; i++)
{
items.Add(new SelectListItem {
Text = CodeList[i].description,
Value = CodeList[i].code_table_name
});
}
ViewData["items"] = items;
return View("CodesAdmin");
}
这是 CodesAdmin aspx 页面/查看代码:
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
CodesAdmin
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("subMenu/ParcelSubMenu"); %>
<h2>CodesAdmin</h2>
<%= Html.DropDownList("items")%>
<input type="button" id="btnGetCodes" name="GetCodes"
value="Get Codes" class="fg-button ui-state-default appButton"
onclick="searchClick()"/>
<div id="CodeAdminPlaceHolder"></div>
<script type="text/javascript">
function searchClick() {
var searchText = $("#items").val();
$.get("/Admin/CodeListing/",
{ identifier: searchText }, function (data) {
$("#CodeAdminPlaceHolder").html(data);
}, "html");
}
</script>
</asp:Content>
现在,这是从按钮单击调用的控制器代码:
public ActionResult CodeListing(string identifier)
{
_entities.Refresh(System.Data.Objects.RefreshMode.StoreWins,
_entities.Address_Type_Codes);
var CodeList = _entities.Address_Type_Codes.
Where(p=>p.active == true).ToList();
return PartialView("ListOfAddrCodes", CodeList);
}
CodeListing 操作实际上根据“标识符”参数处理多个输入。
在“CodeAdminPlaceHolder”div 中呈现的结果部分视图是:
<div id="Div15" class="ui-widget-content">
<table id="CodeListing" class="tablesorter">
<thead>
<tr>
<th></th>
<th>
Short Description
</th>
<th>
Long Description
</th>
<th>
Active
</th>
<th>
Tax Year
</th>
<th>
Note
</th>
</tr>
</thead>
<tbody>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Deactivate",
"DeactivateAddrCode", new {
id=item.address_type_codes_id
}) %>
</td>
<td>
<%: item.short_description %>
</td>
<td>
<%: item.long_description %>
</td>
<td>
<%: item.active %>
</td>
<td>
<%: item.tax_year %>
</td>
<td>
<%: item.note %>
</td>
</tr>
<% } %>
</tbody>
<tfoot>
<tr id="pager">
<td colspan="6">
<img src="/Content/TableSorter/Themes/Default/first.png"
class="firstPage" alt="First" />
<img src="/Content/TableSorter/Themes/Default/prev.png"
class="prevPage" alt="Prev"/>
<input type="text" class="pagedisplay"/>
<img src="/Content/TableSorter/Themes/Default/next.png"
class="nextPage" alt="Next"/>
<img src="/Content/TableSorter/Themes/Default/last.png"
class="lastPage" alt="Last"/>
<select class="pagesize">
<option selected="selected" value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
</td>
</tr>
</tfoot>
</table>
</div>
如果有人单击“停用”链接,则调用此操作:
public ActionResult DeactivateAddrCode(int id)
{
Address_Type_Codes ac1 = _entities.Address_Type_Codes.
First(c => c.address_type_codes_id == id);
ac1.active = false;
_entities.Refresh(System.Data.Objects.RefreshMode.ClientWins,
_entities.Address_Type_Codes);
UpdateModel(ac1);
_entities.SaveChanges();
return RedirectToAction("ListCodeTypes");
}
这会将页面加载到带有下拉框和按钮的开头。
问题是,当我单击按钮加载地址类型时,它不会调用“CodeListing”控制器操作。我通过在“CodeListing”代码中放置一个断点来验证这一点,果然,代码没有被调用。因此,当在下拉列表中进行选择并单击按钮时,它会显示无效数据。如果我在 VS 中停止并重新启动应用程序,数据会正确列出。
CodeListing 操作只被调用一次,我猜缓存的页面被返回。
我们正在使用实体框架进行数据访问,但我认为我们的问题与此无关。