如何asp.net MVC记得我上回发不正确的值? [英] How does asp.net MVC remember my incorrect values on postback?

查看:144
本文介绍了如何asp.net MVC记得我上回发不正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是工作,但如何???

我有一个职位的控制器动作:

 的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult编辑(人人)
{
   BOOL的isValid = ModelState.IsValid;
   等等

Person对象有一个属性生日,DateTime类型。
当我在表单中输入一些无效的数据,说布拉布拉,这是明显的不是一个有效的日期时间,它填补了所有的(其他)人用正确的数据,并用新的空白日期时间的出生日期财产属性。
该BOOL的isValid有假的价值。
到目前为止好。

然后我做到这一点:

 返回查看(P);

和在视图中我有这样的:

 <%= Html.TextBox(出生日期的String.Format({0:绿},Model.BirthDate))%GT;
&所述;%= Html.ValidationMessage(出生日期,*)%>

蚂蚁有说到:我预期模型包含新的空白日期时间,因为我没有把任何新的数据,其次,当查看显示的东西,它必须是一个DateTime,因为 Model.BirthDate 不能装任何东西,但一个DateTime。
但让我吃惊,它显示了布拉布拉价值的文本框! (和它背后的红色*)

这ofcourse是好的,因为用户可以SEEE他输入错了,但怎么可能(布拉布拉)字符串中的日期时间字段被转移到视图?

编辑:
该ModelState中的信息在这里帮了我很多。
我也注意到,在MVC 2,当你创建自己的模板
Html.EditorFor()
你必须自己实现此行为。
我创建了一个

  DateTime.ascx

在/查看/共享/ EditorTemplates文件夹,并在那里我不得不检查,如果有此属性值的ModelState错误,如果是这样,显示模型中的数据代替无效数据。

所以,在视图中我用这个:

 <%= Html.LabelFor(型号=> model.DateOfBirth)%GT;

和在DateTime.ascx我用这个:

 <%
布尔invalidData = FALSE;
字符串参数propertyName = ViewData.ModelMetadata.PropertyName;ModelState中毫秒= ViewData.ModelState [propertyName的];
如果(MS!= NULL)
{
   invalidData = ms.Errors.Count> 0;
}
字符串valueToshow = invalidData? ViewData.ModelState [参数propertyName] .Value.AttemptedValue:的String.Format({0:绿},模型);
%GT;
<输入类=文本框单行ID =<%= propertyName的%GT; NAME =&下;%= propertyName的%gt;中TYPE =文本VALUE =<%= valueToshow%GT; />


解决方案

的ModelState举行KeyValuePairs与键为字段名和值每个表​​单元素是你把在外地。然后将HTML助手看在ModelState中,如果你没有明确指定一个值,他们会从ModelState中拉出值。

This is working, but how???

I have a controller action for a post:

[AcceptVerbs(HttpVerbs.Post )]
public ActionResult Edit(Person person)
{
   bool isvalid = ModelState.IsValid;
   etc.

The Person object has a property BirthDate, type DateTime. When i enter some invalid data in the form, say 'blabla' which is obvious not a valid Datetime, it fills all the (other) Person properties with the correct data and the BirthDate property with a new blank DateTime. The bool isvalid has the value 'false'. So far so good.

Then i do this:

return View(p);

and in the view i have this:

<%= Html.TextBox("BirthDate", String.Format("{0:g}", Model.BirthDate)) %>
<%= Html.ValidationMessage("BirthDate", "*") %>

Ant there it comes: i EXPECTED the model to contain the new, blank DateTime because i didn't put any new data in. Second, when the View displays something, it must be a DateTime, because Model.BirthDate can't hold anything but a DateTime. But to my surprise, it shows a textbox with the 'blabla' value! (and the red * behind it)

Which ofcourse is nice because the user can seee what he typed wrong, but how can that (blabla)string be transferred to the View in a DateTime field?

EDIT: The ModelState info helped me a lot here. I also notices that in MVC 2, that when you create your own Template for Html.EditorFor() you have to implement this behaviour yourself. I created a

DateTime.ascx 

in the /views/shared/EditorTemplates folder, and in there i had to check if there was a modelstate error for this property value, and if so, show the invalid data in stead of the Model data.

So in the view i use this:

 <%= Html.LabelFor(model => model.DateOfBirth) %>

and in the DateTime.ascx i use this:

<%
bool invalidData = false;
string propertyName = ViewData.ModelMetadata.PropertyName;

ModelState ms = ViewData.ModelState[propertyName];
if (ms != null)
{
   invalidData = ms.Errors.Count > 0;
}
string valueToshow = invalidData ? ViewData.ModelState[propertyName].Value.AttemptedValue : String.Format("{0:g}", Model);
%>
<input class="text-box single-line" id="<%= propertyName %>" name="<%= propertyName %>" type="text" value="<%= valueToshow %>" />

解决方案

ModelState holds KeyValuePairs for every form element with the key being the field name and the value is what you put in the field. Then the Html Helpers look in ModelState and if you don't explicitly specify a value, they will pull the value from ModelState.

这篇关于如何asp.net MVC记得我上回发不正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆