使用AJAX提交部分页面时出错
本文关键字:出错 分页 AJAX 提交部 使用 | 更新日期: 2023-09-27 18:03:56
最近,我经常在开发中遇到错误,但在生产中却没有,这就是其中一个。当使用AJAX提交部分页面时,我得到了错误"500内部服务器错误". Firebug捕获的细节:
异常细节
控制器:TimeKeeper
动作:编辑
消息:没有类型为'IEnumerable
StackTrace:
Exception: at System.Web.Mvc.Html.SelectExtensions。GetSelectData(HtmlHelper, HtmlHelper,字符串名称)在System.Web.Mvc.Html.SelectExtensions。SelectInternal(HtmlHelper, HtmlHelper, ModelMetadata, metadata, String optionLabel, String name, IEnumerable ' 1 selectList, Boolean allowMultiple, IDictionary ' 2 htmlAttributes)在System.Web.Mvc.Html.SelectExtensions。下拉列表(HtmlHelper, HtmlHelper,字符串名称,IEnumerable ' 1选择列表,字符串optionLabel,对象htmlAttributes)at asp . _page_views_timekeeper _edit_cshtml . execute () in c:'inetpub'wwwroot'[appname]'Views'TimeKeeper'_Edit。cshtml: 64行在System.Web.WebPages.WebPageBase.ExecutePageHierarchy ()在System.Web.Mvc.WebViewPage.ExecutePageHierarchy ()在System.Web.WebPages.WebPageBase。ExecutePageHierarchy(WebPageContext, pageContext, TextWriter, WebPageRenderingBase)在System.Web.Mvc.RazorView。RenderView(ViewContext, ViewContext, TextWriter, Object实例)在System.Web.Mvc.BuildManagerCompiledView。渲染(ViewContext, ViewContext, TextWriter)在System.Web.Mvc.ViewResultBase。ExecuteResult (ControllerContext上下文)在System.Web.Mvc.ControllerActionInvoker。InvokeActionResult(ControllerContext ControllerContext, ActionResult ActionResult)在System.Web.Mvc.ControllerActionInvoker。InvokeActionResultFilterRecursive(IList ' 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext, ControllerContext, ActionResult ActionResult)在System.Web.Mvc.ControllerActionInvoker。InvokeActionResultFilterRecursive(IList ' 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext, ControllerContext, ActionResult ActionResult)在System.Web.Mvc.ControllerActionInvoker。InvokeActionResultWithFilters(ControllerContext, ControllerContext, IList ' 1 filters, ActionResult ActionResult),在System.Web.Mvc.Async.AsyncControllerActionInvoker。你们的在c__DisplayClass21灵活;祝辞c__DisplayClass2b灵活;BeginInvokeAction> b__1c ()at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult)
_Edit。cshtml(部分):
@model appname.ViewModels.TimeKeeperEditViewModel
<link href="@Url.Content("~/Content/datepicker.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/datepicker3.css")" rel="stylesheet" type="text/css" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Edit Time Keeper</h4>
</div>
@using (Ajax.BeginForm("Edit", "TimeKeeper",
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnComplete = "editTimeKeeperComplete",
UpdateTargetId = "timeKeeperList"
}))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.TimeKeeperID)
@Html.HiddenFor(model => model.BillingID)
<div class="modal-body">
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-4">
<label for="billingRate" class="control-label">Billing Rate</label>
</div>
<div class="col-md-8">
@Html.EditorFor(model => model.BillingRate, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<label for="billingStartDate" class="control-label">Billing Start Date</label>
</div>
<div class="col-md-8">
@Html.EditorFor(model => model.BillingStartDate, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<label for="status" class="control-label">Status</label>
</div>
<div class="col-md-8">
@Html.DropDownList("StateID", null, String.Empty, new { @class = "form-control" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="btnClose">Close</button>
<button type="submit" class="btn btn-primary" id="btnSave">Save changes</button>
</div>
}
<script type="text/javascript">
$(document).ready(function () {
$("#BillingStartDate").datepicker({
format: "dd/mm/yyyy",
todayBtn: "linked",
autoclose: true,
todayHighlight: true
});
});
</script>
填充StateID的代码:
private void PopulateStateDropDownList(object selectedState = null)
{
var statesQuery = from s in db.States
where s.RowStatus == true && s.StateGroupID == 1
orderby s.Name
select new
{
StateID = s.StateID,
Name = s.Name
};
ViewBag.StateID = new SelectList(statesQuery, "StateID", "Name", selectedState);
}
Post action code:
[HttpPost]
public ActionResult Edit(TimeKeeperEditViewModel timeKeeperEditVM)
{
try
{
if (ModelState.IsValid)
{
LogHelper.logger.Info("Model Valid");
TimeKeeper timeKeeper = db.TimeKeepers.Find(timeKeeperEditVM.TimeKeeperID);
timeKeeper.BillingRate = timeKeeperEditVM.BillingRate;
timeKeeper.BillingStartDate = timeKeeperEditVM.BillingStartDate;
timeKeeper.BillingEndDate = timeKeeperEditVM.BillingEndDate;
timeKeeper.StateID = timeKeeperEditVM.StateID;
LogHelper.logger.Info("Save");
db.Entry(timeKeeper).State = EntityState.Modified;
db.SaveChanges();
LogHelper.logger.Info("Get Timekeepers");
Billing billing = db.Billings.Find(timeKeeperEditVM.BillingID);
BillingTimeKeeperViewModel billingVM = new BillingTimeKeeperViewModel()
{
BillingID = billing.BillingID,
TimeKeepers = billing.TimeKeepers
};
LogHelper.logger.Info("Return");
return PartialView("_List", billingVM.TimeKeepers);
}
else
{
LogHelper.logger.Info("Model is not Valid");
return PartialView("_Edit");
}
}
catch (Exception ex)
{
LogHelper.logger.Error(ex);
return RedirectToAction("Index", "Error", ex);
}
}
我正在使用NLog查看问题。没有错误,但是ModelState.IsValid
为假
我将log放在if (ModelState.IsValid)
之前,以打印出BillingRate, BillingStartDate, BillingEndDate和StateID。这是由于BillingEndDate是空的,如果我填写日期超过12,例如13/1/2014,但如果我填写12/1/2014 ModelState.IsValid
是真的。
所以我的结论,这是由日期格式引起的。问题是我禁止更改服务器日期格式。任何建议吗?
通过viewbag传递下拉列表通常是不好的。我建议把它放入模型
public List<SelectListItem> StateList { get; set; }
然后用数据库调用
填充这个列表foreach(var temp in statesQuery){
model.StateList.Add(new SelectListItem() { Text = temp.Name, Value = temp.StateID });
}
然后在视图中将下拉列表更改为
@Html.DropDownListFor(x => x.StateID, Model.StateList)
可以通过设置模型来设置控制器上的下拉菜单。StateID和从下拉列表中选择的值将绑定到post back上的字段