I'm having a problem with model binding. If I do use the prefix in the CopyToModelStateDictionary method, I don't get the styling or validation messages for the invalid controls. If I don't use the prefix, I get the styling and validation messages, but the page crashes if the user hasn't made a selection from a dynamically created list of radio buttons. How can I get both? I don't understand what is happening here.
[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);
}
}
}
Error Message
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: }
Stack Trace:
[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
Thanks for reading my question!