ASP.Net MVC RC2 ValidationMessage and form field conflict?

爷,独闯天下 提交于 2019-12-07 20:02:37

问题


I am having problems with MVC RC2 where upon validation failure, the failed field will throw a NullReferenceException when the view is passed back to the user.

A short term solution was found: which was to rename the Html.ValidationMessage to be different than the Target Form Field. This works!

BUT now the automatic highlighting is disconnected from the input field. (Out of the box behavour is to change the target field's CSS Class which makes it standout)

So...

What is the actual problem with my code? AND Why wont it allow my ValidationMessage and Form fields share the same names?

The code is throwing NullReferenceException when the following code is run:

View Code

<% using (Html.BeginForm()) { %>
   <fieldset>
     <h5>Terms and Conditions</h5>
     <p>
       <%= Html.CheckBox("Terms", false)%>
       <%= Html.ValidationMessage("Terms")%>
       I agree to the <a href="/signup/terms">Terms & Conditions.</a>
     </p>
   </fieldset>
   <input class="signup_button" type="submit" title="Sign Up" value="" />
<% } %>
<%= Html.ValidationSummary("Sign up wasn't successful.")%>

Controller Code

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
    bool Terms = form["Terms"].ToString() == "true,false" ? true : false;

    if (Terms)
    {
        return RedirectToAction("Success", "Signup");
    }
    else 
    {
        ModelState.AddModelError("Terms", "Please agree to the Terms");
        ModelState.AddModelError("_FORM", "Terms not checked");
    }
    return View();
}

I can get the code to work if I omit the following:

ModelState.AddModelError("Terms", "Please agree to the Terms");

But with this, the checkbox throws the Null reference exception.

Any Ideas?


回答1:


Try this:

else 
{
    ModelState.AddModelError("Terms", "Please agree to the Terms");
    ModelState.SetModelValue("Terms", form.ToValueProvider()["Terms"]);
    ModelState.AddModelError("_FORM", "Terms not checked");
}

If that doesn't work, then please post the full stack for the exception.




回答2:


Looks like a short term answer is simply to rename the html.ValidationMessage to something else

<%= Html.ValidationMessage("TermsError")%>

And make sure the control uses the same name when adding an Error state

ModelState.AddModelError("TermsError", "Please agree to the Terms");

This fixes the issue for me. Still, I'm left wondering... what is the best naming convention for using the html.ValidationMessage?




回答3:


In this case, why are you passing in form collection? Why not do this?

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(bool terms)
{
    if (terms)
    {
        return RedirectToAction("Success", "Signup");
    }
    else 
    {
        ModelState.AddModelError("Terms", "Please agree to the Terms");
        ModelState.AddModelError("_FORM", "Terms not checked");
    }
    return View();
}

That ought to work just fine.



来源:https://stackoverflow.com/questions/633365/asp-net-mvc-rc2-validationmessage-and-form-field-conflict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!