简体   繁体   English

ASP.NET MVC-视图呈现的值不存在吗?

[英]ASP.NET MVC - View is rendering values that do not exist?

I have this: 我有这个:

model.PasswordField = null;
return View("MainLogin", model);

My model contains LoginField and PasswordField . 我的模型包含LoginFieldPasswordField I leave LoginField untouched, but I erase PasswordField . 我保持LoginField不变,但删除了PasswordField Now, when MainLogin view is rendered, the field password has a value from my previous request! 现在,在呈现MainLogin视图时,字段密码具有我先前请求的值! Why? 为什么?

Update: Added my view code: 更新:添加了我的视图代码:

<div class="editor-field">
                @Html.TextBoxFor(model => model.PasswordField, new { placeholder = "Password" })
                @Html.ValidationMessageFor(model => model.PasswordField)
            </div>

You are changing the value in the Model but the HtmlHelper methods check the ModelState first to display the value, on postback. 您正在更改模型中的值,但HtmlHelper方法首先检查ModelState以在回发时显示该值。 This is by design. 这是设计使然。 To display the modified value, you need to clear the ModelState using, 要显示修改后的值,您需要使用以下命令清除ModelState,

ModelState.Clear();

in the load of your form you can create a function clean() where you put an initialization of field you want to be empty for example 在表单加载中,您可以创建一个函数clean(),在其中放置您想为空的字段的初始化

clean()
{

textlabel.setvalue="enter text ";
field1.empty();

passwordField.empty();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM