ASP.Net MVC RC2 ValidationMessage and form field conflict?

*爱你&永不变心* 提交于 2019-12-06 09:22:31

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.

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?

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.

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