C# ASP.NET MVC模型绑定
我的模型绑定有问题。如果我在CopyToModelStateDictionary方法中使用前缀,我不会得到无效控件的样式或验证消息。如果我不使用前缀,我会得到样式和验证消息,但是如果用户没有从动态创建的单选按钮列表中进行选择,页面就会崩溃。我怎样才能两者兼得?我不明白这里发生了什么C# ASP.NET MVC模型绑定,c#,asp.net-mvc,model-binding,C#,Asp.net Mvc,Model Binding,我的模型绑定有问题。如果我在CopyToModelStateDictionary方法中使用前缀,我不会得到无效控件的样式或验证消息。如果我不使用前缀,我会得到样式和验证消息,但是如果用户没有从动态创建的单选按钮列表中进行选择,页面就会崩溃。我怎样才能两者兼得?我不明白这里发生了什么 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection collection) {
[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: }
谢谢你阅读我的问题 这可能会有所帮助 您正在添加一个模式错误,因此MVC正在尝试检索尝试的值,但由于您没有添加它,因此失败
这可能会有所帮助 您正在添加一个模式错误,因此MVC正在尝试检索尝试的值,但由于您没有添加它,因此失败
感谢您提供这些链接。我必须做更多的研究才能发布我的解决方案。这些链接非常有用。另外,如果有人需要更多帮助,请在StackOverflow中搜索“ValueProvider”。我将在进行更多测试后使用我的解决方案进行编辑。感谢这些链接。我必须做更多的研究才能发布我的解决方案。这些链接非常有用。此外,如果有人需要更多帮助,请在StackOverflow中搜索“ValueProvider”。我将在进行更多测试后使用我的解决方案进行编辑。
[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