[英]input tag helper for ASP.NET Core doesn't change its text to the bound model field
I'm using the input
tag helper for my Razor view like so: 我在Razor视图中使用
input
标签助手,如下所示:
<input type="text" asp-for="MsgFromFilter" id="txtMsgFromFilter" />
So this should be bound to my model's MsgFromFilter
string field. 因此,这应该绑定到我模型的
MsgFromFilter
字符串字段。 Although, when I submit the form, this field in my model is bound to the value in the textbox, it doesn't work the other way round: when I set the value of this field in my controller, the view doesn't set that as the default value of the text input element; 虽然,当我提交表单时,模型中的该字段绑定到文本框中的值,但反之则行不通:当我在控制器中设置此字段的值时,视图未设置作为文本输入元素的默认值; it just maintains the last value submitted in that
input
field. 它只是保持在该
input
字段中提交的最后一个值。 How can I get the text input
to default its value on page load to the value in the model rather than the last value that was submitted in the input
? 我怎样才能获得文本
input
到它在页面加载值默认为在模型中,而不是在提交的最后一个值的数值input
?
Thanks to @KirkLarkin I discovered that to set the defaults for the input
fields rendered by the Razor view, I need to set ModelState
. 感谢@KirkLarkin,我发现要设置Razor视图呈现的
input
字段的默认值,我需要设置ModelState
。 So in my controller I have to do two things, set ModelState
and the model itself: 因此,在我的控制器中,我必须做两件事,设置
ModelState
和模型本身:
if (btnReset) {
ModelState.Remove(nameof(model.MsgFromFilter));
model.MsgFromFilter = null;
}
This works for resetting the value of that model field as well as the Razor view input element to blank. 这可用于将该模型字段以及Razor视图输入元素的值重置为空白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.