不知道这是怎么回事。
我的控制器方法看起来像这样:
[HttpGet]
public ActionResult RequestAppointment()
{
var appointmentRequest = new AppointmentRequest
{
Stylists = _repository.Stylists // <-- Debugging shows that Stylists IS being populated here
};
return View(appointmentRequest);
}
[HttpPost]
public ActionResult RequestAppointment(AppointmentRequest appointmentRequest)
{
if(ModelState.IsValid)
{
// Process...
return RedirectToAction("Confirmation");
}
return View(appointmentRequest);
}表单如下所示:
@model MyDomain.Models.AppointmentRequest
@using(Html.BeginForm("RequestAppointment", "Appointment" FormMethod.Post))
{
// This following line throws the exception:
@Html.DropDownListFor(x => x.Stylist,
Model.Stylists.Select(x => new SelectListItem{ Text = x.Name, Value = x.Name })))
<input type="submit" value="Make Request" />
}下拉列表中填充了正确的文本和值。但是当表单提交时,就会抛出异常。到底怎么回事?
堆栈跟踪:
[ArgumentNullException: Value cannot be null.
Parameter name: source]
System.Linq.Enumerable.Select(IEnumerable`1 source, Func`2 selector) +6396316
ASP._Page_Views_Appointment_RequestAppointment_cshtml.Execute() in c:\Projects\OasisSalon\OasisSalon.Mvc\Views\Appointment\RequestAppointment.cshtml:9
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +157
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +825460
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +825488
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375发布于 2012-04-02 13:53:43
如果要重新显示相同的视图,则必须在POST操作中初始化Stylists属性:
[HttpPost]
public ActionResult RequestAppointment(AppointmentRequest appointmentRequest)
{
if(ModelState.IsValid)
{
// Process...
return RedirectToAction("Confirmation");
}
appointmentRequest.Stylists = _repository.Stylists;
return View(appointmentRequest);
}您会得到一个NRE,因为您试图在视图中使用Model.Stylists呈现视图中的DropDownList,但是很明显,在执行POST操作之后,此属性为null,因为您从未对其赋值,并且它的值不会自动填充,因为发送到POST请求的所有内容都是下拉列表( Stylist属性)的选定值。
发布于 2012-04-02 02:53:52
我的心理调试技能告诉我,您在没有创建模型的情况下从POST操作返回View()。
发布于 2012-04-02 03:09:37
ASP.NET MVC没有ViewState。因此,它不能像ASP.ENT Webforms那样跨回发保留DropDown列表的值。所以我猜,在HttpPost操作方法中,您将样式返回给视图,但是appointmentRequest属性为空。在将appointmentRequest返回到视图之前,您可能需要重新加载它。
https://stackoverflow.com/questions/9967224
复制相似问题