添加 html5 属性后模型绑定停止
我有一个视图,它使用 TextBoxFor 辅助方法来编辑模型。像这样:
<div class="editor-field">
<%:Html.TextBoxFor(model => model.Property, new { @class = "mybox" })%>
<%: Html.ValidationMessageFor(model => model.Property)%>
</div>
所以,Modelbinder 工作正常,我可以编辑和保存我的模型。
当我像这样添加 html5 属性“类型”和“步骤”时
<div class="editor-field">
<%:Html.TextBoxFor(model => model.Property, new { @class = "mybox",@type = "number", @step = "1" })%>
<%: Html.ValidationMessageFor(model => model.Property)%>
</div>
,我看到漂亮的数字控件而不是文本框,但是模型绑定会刹车,每当我更改属性时,我都会在模型中收到 0。提交时的属性。
我的代码有什么问题吗?
这是我的简单模型,
public class Model
{
[DisplayName("Property")]
public int Property
{
get;
set;
}
}
内森,我对问题的解释在这里并不准确。这里 在 Chrome/Safari 中添加 html5 属性后 Ajax 表单中断 是更好的描述和解决方法。
I have a view and it uses TextBoxFor helper method to edit model. Like this:
<div class="editor-field">
<%:Html.TextBoxFor(model => model.Property, new { @class = "mybox" })%>
<%: Html.ValidationMessageFor(model => model.Property)%>
</div>
So, Modelbinder works fine and I can edit and save my model.
When I add html5 attributes 'type' and 'step' like this
<div class="editor-field">
<%:Html.TextBoxFor(model => model.Property, new { @class = "mybox",@type = "number", @step = "1" })%>
<%: Html.ValidationMessageFor(model => model.Property)%>
</div>
I see beautiful numeric control instead of textbox, but modelbinding brakes and whenever I change my Property I recieve 0 in my model.Property on submit.
What's wrong with my code?
That's my simple model
public class Model
{
[DisplayName("Property")]
public int Property
{
get;
set;
}
}
Nathan, my explanation of the problem is not precise here. Here Ajax Form breaks after adding html5 attributes in Chrome/Safari is a better description and workaround.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下:
http://deanhume.com/Home/ BlogPost/asp-net-mvc-html5-toolkit/29
根据您的代码,这对我有用:
控制器:
模型:
视图:
当我单击提交按钮时,会收到在数字输入控件中选择的数字由控制器并输出到浏览器。
我们需要查看更多代码才能提供更多帮助。
Give this a try:
http://deanhume.com/Home/BlogPost/asp-net-mvc-html5-toolkit/29
Based on your code, this works for me:
Controller:
Model:
View:
When I click my submit button, the number selected in my numeric input control is received by the controller and output to the browser.
We'll need to see more code to be more helpful.