繁体   English   中英

ASP.NET MVC Model 绑定

[英]ASP.NET MVC Model Binding

我在使用 model 绑定时遇到问题。 如果我确实在 CopyToModelStateDictionary 方法中使用了前缀,我就不会得到无效控件的样式或验证消息。 如果我使用前缀,我会收到样式和验证消息,但如果用户没有从动态创建的单选按钮列表中进行选择,页面就会崩溃。 我怎样才能得到两者? 我不明白这里发生了什么。

    [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

感谢您阅读我的问题!

这可能会有所帮助。

您正在添加模态错误,因此 MVC 正在尝试检索尝试的值,但因为您没有添加它,所以它失败了。

使用 ModelState 验证表单

ASP.NET MVC - Html.Textbox() 抛出“对象引用未设置到对象的实例”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM