2009-06-17 235 views
1

我遇到了模型綁定的問題。如果我在CopyToModelStateDictionary方法中使用前綴,我沒有得到無效控件的樣式或驗證消息。如果我不要使用前綴,我會得到造型和驗證消息,但如果用戶沒有從動態創建的單選按鈕列表中進行選擇,頁面就會崩潰。我怎麼能得到這兩個?我不明白這裏發生了什麼。ASP.NET MVC模型綁定

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Edit(int id, FormCollection collection) 
    { 
     Request request = requestRepository.GetById(id); 
     UpdateModel(request); 
     Helpers.CopyToModelStateDictionary(request.GetRuleViolations(), ModelState, "request"); 
     ... 
     if (!ModelState.IsValid) 
     { 
      return View("Edit_Requestor", request); 
     } 
    } 

    public static void CopyToModelStateDictionary(NameValueCollection errors, ModelStateDictionary modelState, string prefix) 
    { 
     foreach (string key in errors) 
     { 
      foreach (string value in errors.GetValues(key)) 
      { 
       modelState.AddModelError(prefix + "." + key, value); 
      } 
     } 
    } 

錯誤消息

Line 9: foreach (ProblemType problemType in problemTypes) 
Line 10: { 
Line 11:  Writer.Write(Html.RadioButton("ProblemType", problemType.Value)); 
Line 12:  Writer.Write(problemType.Text + "<br/>"); 
Line 13: } 

堆棧跟蹤:

[NullReferenceException: Object reference not set to an instance of an object.] 
System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) +63 
System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary`2 htmlAttributes) +328 
System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value, Boolean isChecked, IDictionary`2 htmlAttributes) +193 
System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value, IDictionary`2 htmlAttributes) +282 
System.Web.Mvc.Html.InputExtensions.RadioButton(HtmlHelper htmlHelper, String name, Object value) +61 
ASP.views_request_lstproblemtype_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Web\Views\Request\lstProblemType.ascx:11 
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256 
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 
System.Web.UI.Control.Render(HtmlTextWriter writer) +10 
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 
System.Web.UI.Page.Render(HtmlTextWriter writer) +29 
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59 
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266 

感謝您閱讀我的問題!

回答