TextBox 用于显示初始值,而不是从代码更新的值 [英] TextBoxFor displaying initial value, not the value updated from code
问题描述
我有一个显示值的 MVC 应用程序.这是控制器:
I have an MVC application that displays a value. This is the controller:
public ActionResult Index(DataSites DataSiteList)
{
if (DataSiteList.Latitude != null)
{
DataSites test = new DataSites();
test.Latitude = "LATITUDE";
return View(test);
}
return View(DataSiteList);
}
public ActionResult SomeInformation()
{
DataSites test1 = new DataSites();
test1.Latitude = "LATITUDE2";
return RedirectToAction("Index", test1);
}
视图:
@model miniproj2.Models.DataSites
<p>
@Html.TextBoxFor(x => x.Latitude)
</p>
和模型:
public class DataSites
{
public string Latitude { get; set; }
}
当我转到/Home/SomeInformation 时,DataSites
' Latitude
属性设置为LATITUDE2".然后重定向到控制器中的 Index()
操作,将属性设置为纬度"并返回视图.
When I go to /Home/SomeInformation, the DataSites
' Latitude
property is set to "LATITUDE2". Then redirects to the Index()
action in the controler, sets the property to "LATITUDE" and returns the view.
当它显示视图时,它会显示重定向中设置的值LATITUDE2".不应该显示纬度"吗?
When it shows the view, it displays the value "LATITUDE2" as set in the redirect. Shouldn't "LATITUDE" be displayed?
推荐答案
你的问题是(一步一步)
Your problem is (step by step)
- 您的
SomeInformation()
方法设置了test1.Latitude
的值到纬度2". - 然后使用重载将该模型传递给
Index()
方法RedirectToAction
接受一个对象.在内部这使用反射以基于属性构建RouteValueDictionary
您的模型(在本例中只是latitude="LATITUDE2"
). - 当您点击
Index
方法时,模型由DefaultModelBinder
绑定,现在DataSiteList.Latitude
的值为LATITUDE2"(这就是为什么你输入if
块) - 在绑定过程中,
DefaultModelBinder
设置了ModelState
Latitude
的值到LATITUDE2".任何尝试设置Latitude
的值现在被忽略,因为视图使用ModelState
值来呈现控件.
- Your
SomeInformation()
method sets the value oftest1.Latitude
to "LATITUDE2". - You then pass that model to your
Index()
method using the overload ofRedirectToAction
that accepts an object. Internally this uses reflection to build aRouteValueDictionary
based on the properties of your model (in this case its simplylatitude="LATITUDE2"
). - When you hit the
Index
method the model is bound by theDefaultModelBinder
and now the value ofDataSiteList.Latitude
is "LATITUDE2" (which is why you enter theif
block) - In the process of binding, the
DefaultModelBinder
sets theModelState
value ofLatitude
to "LATITUDE2". Any attempts to set the value ofLatitude
are now ignored because the view usesModelState
value to render the control.
不清楚你在这里想做什么.您可以通过添加 ModelState.Clear();
作为 Index()
方法的第一行,使其按预期工作.这会清除所有现有的 ModelState
值,您现在可以将该值设置为LATITUDE".
It not clear what your trying to do here. You can make it work as you expect by adding ModelState.Clear();
as the first line of your Index()
method. This clears all existing ModelState
values an you can now set the value to "LATITUDE".
但是你的 if
块没有意义.也许您只是在做某种测试,但您也可以从 Index()
方法中删除参数,并在该方法中初始化 DataSites
的新实例.
But your if
block makes no sense. Perhaps you were just doing some kind of test, but you may as well remove the parameter from the Index()
method and just initialize a new instance of DataSites
in the method.
编辑
提供更多关于为什么在设置 ModelState
后更新模型属性没有影响的信息.
To give a bit more information as to why updating a model property has no affect once ModelState
has been set.
假设您有一个表单来收集用户信息,其中模型包含 int Age
.用户被要求输入他们的年龄,有人输入我下周五岁!".当然这不会绑定到 int 所以 DefaultModelBinder
添加值(attemptedValue
)并添加一个 ModelStateError
.
Imagine you have a form to collect user information where the model contains int Age
. The user is asked to enter their age and someone enters "I'm five next week!". Of course this wont bind to an int so the DefaultModelBinder
adds the value (the attemptedValue
) and adds a ModelStateError
.
返回视图时,它通常会显示一条错误消息,例如字段年龄必须是数字".如果呈现控件的 html 帮助程序使用模型值,则它将显示0"(int 的默认值).如果用户在文本框中看到0",然后在其旁边看到一条消息,说明它必须是一个数字(什么!但零是一个数字,我输入的内容到底发生了什么?
When the view is returned it will typically display an error message such as "The field Age must be a number". If the html helper rendering the control used the model value, then it would display "0" (the default value for int). It would be somewhat confusing for the user to see "0" in the textbox and next it a message saying it must be a number (What! but zero is a number and what the heck happened to what I entered?). So instead, the helper uses the value from ModelState
and now the users sees "I'm five next week!" and an associated error message that makes sense for the value.
因此,即使您认为这不合逻辑",这种行为实际上也有一定的逻辑性.
So even though you thoughts were that "its not logical", there is actually some logic to this behavior.
这篇关于TextBox 用于显示初始值,而不是从代码更新的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!