TextBoxFor显示初始值,而不是从价值code更新 [英] 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
天气预报
属性设置为LATITUDE2。然后重定向到在CONTROLER的指数()
行动,将该属性设置为纬度,并返回视图。
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
的价值
为LATITUDE2。 - 您那么这个模型传递到你的
指数()
使用方法的重载
的RedirectToAction
接受的对象。这在内部使用
反射打造RouteValueDictionary
按财产
你的模型(在这种情况下,它只是latidute =LATITUDE2
)。 - 当你点击
首页
方法的模型是由绑定的DefaultModelBinder
现在的值DataSiteList.Latitude
是LATITUDE2(这就是为什么你进入
在如果
块) - 在结合中,
DefaultModelBinder
进程设置纬度
来LATITUDE2的的ModelState
值。任何试图设置纬度
的价值现在被忽略,因为该视图的用途的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 simplylatidute="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();
作为第一线的指数()
方法。这将清除所有现有的的ModelState
值的,你现在可以将值设置为纬度。
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".
但是,你的如果
块是没有意义的。也许你只是在做一些测试,但你不妨请从指数()
方法的参数,只是初始化的新实例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年龄
。用户被要求输入自己的年龄和有人进入我5下个星期!。当然,这不会绑定到一个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,在文本框和旁边一条消息说,它必须是一个数字(的什么!但零是一个数字,你到底发生了我输入的内容?)。所以取而代之,辅助使用来自的ModelState
的价值,现在用户看到我5下个星期!和一个相关的错误消息,有意义的值。
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.
这篇关于TextBoxFor显示初始值,而不是从价值code更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!