DropDownListFor抛出ArgumentNullException

时间:2012-04-01 18:51:25

标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor

不确定这里发生了什么......

我的控制器方法如下所示:

[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

4 个答案:

答案 0 :(得分:3)

我的通灵调试技巧告诉我你在没有创建模型的情况下从POST动作返回View()

答案 1 :(得分:1)

ASP.NET MVC没有ViewState。所以它不能像ASP.ENT Webforms那样在回发之间保留DropDown列表的值。所以我想,在HttpPost Action方法中,您将appointmentRequest返回给View,但是Stylists属性为null。在将appointmentRequest返回到视图之前,您可能需要重新加载它。

答案 2 :(得分:1)

我想这里发生的事情是你的Post动作中Stylists为空。它在模型绑定期间不受约束,因为下拉列表适用于Stylist,而不是Stylists

在将Stylists模型传递给视图之前,您需要重建appointmentRequest属性。

答案 3 :(得分:1)

如果您打算重新显示相同的视图,则必须在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动作执行后为空,因为你从未分配它并且它的值没有被填充自动,因为发送到POST请求的所有内容都是下拉列表的选定值(Stylist属性)。