验证使用的ModelState形式 [英] Validating form using ModelState
问题描述
我想验证使用的ModelState一般的方式在asp.net/mvc教程相同的形式。不过我没有我自己的数据库或他们的对象,并希望来验证的FormCollection。我不知道,怎样的ModelState工作,所以也许我在黑暗中拍摄我,但这里是code,即崩溃:
I am trying to validate a form using ModelState generally the same way as in tutorials on asp.net/mvc. However I dont have my own database or their objects and want to validate against Formcollection. I am not sure, how ModelState works, so maybe I am shooting in the dark, but here is the code, that crashes:
<%= Html.TextBox(Html.En code(atr_+ name.Key),计算机[atr_+ name.Key])%>
&所述;%= Html.ValidationMessage(atr_+ name.Key,*)%>
<%=Html.TextBox(Html.Encode("atr_" + name.Key), ViewData["atr_" + name.Key])%> <%=Html.ValidationMessage("atr_" + name.Key, "*")%>
和的NullReferenceException我得到的文本框:
and NullReferenceException I get on the TextBox:
System.NullReferenceException:对象不设置到对象的实例。在System.Web.Mvc.HtmlHelper.GetModelStateValue(字符串键,类型destinationType)在System.Web.Mvc.Html.InputExtensions.InputHelper(的HtmlHelper的HtmlHelper,inputType下inputType下,字符串名称,对象的值,布尔useViewData,布尔器isChecked,布尔SETID在System.Web.Mvc.Html.InputExtensions.TextBox(的HtmlHelper的HtmlHelper,字符串名称,对象的值,IDictionary的 2 htmlAttributes)在系统布尔isExplicitValue,IDictionary的 2 htmlAttributes)。 Web.Mvc.Html.InputExtensions.TextBox在C(的HtmlHelper的HtmlHelper,字符串名,对象值)ASP.views_authorized_account_aspx .__ RenderContent1(HtmlTextWriter的__w,控制parameterContainer):\\用户\\ Trimack \\文档\\ Visual Studio 2008的\\项目\\ GuestManager \\的AccountManager \\查看\\授权\\ Account.aspx:行61 *
System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Mvc.HtmlHelper.GetModelStateValue(String key, Type destinationType) at System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, IDictionary2 htmlAttributes) at System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value, IDictionary
2 htmlAttributes) at System.Web.Mvc.Html.InputExtensions.TextBox(HtmlHelper htmlHelper, String name, Object value) at ASP.views_authorized_account_aspx.__RenderContent1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Trimack\Documents\Visual Studio 2008\Projects\GuestManager\AccountManager\Views\Authorized\Account.aspx:line 61*
任何想法?还是我完全错了?
Any ideas? Or am I completely wrong?
Trimack
推荐答案
,MVC框架将尝试找到它发现每个错误的AttemptedValue。因为你没有添加它们,MVC会抛出异常。
So, for every error you add with ModelState.AddModelError() and call the View again, MVC Framework will try to find an AttemptedValue for every error it finds. Because you didn't add them, MVC will throw an exception.
http://forums.asp.net/p/1396019/3006051.aspx
如果有错误,那么你还必须将模型值以及模态误差
If there are errors then you must also set the model value as well as the modal error
ModelState.AddModelError("Some_Key","Show some error message");
ModelState.SetModelValue("Some_Key", ValueProvider["Some_Key"]);
这篇关于验证使用的ModelState形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!