ASP.NET MVC 5呈现隐藏输入不同的bool值 [英] ASP.NET MVC 5 renders different bool value for hidden input

查看:78
本文介绍了ASP.NET MVC 5呈现隐藏输入不同的bool值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下视图模型:

public class FooViewModel
{
    public bool IsBoolValue { get; set; }
}

和这样的观点:

<input type="hidden" id="Whatever" data-something="@Model.IsBoolValue" value="@Model.IsBoolValue" />

隐藏的输入字段的输出是这样的:

The output of the hidden input field is this:

&LT;输入类型=隐藏ID =不管数据的东西=真值=值&GT;

为什么在属性未设置​​为,而数据 - 东西属性是什么?

How come the value attribute is not set toTrue, but the data-something attribute is?

有没有MVC 5的改变,将导致此,因为在我的MVC应用程序4不会出现这个问题。

Is there a change in MVC 5 that would cause this, since in my MVC 4 apps this problem does not occur.

推荐答案

我想我已经想通了。

我相信剃刀的ViewEngine秉承设定布尔属性的HTML 5的方式,如下所述:

I believe the Razor viewengine is adhering to the HTML 5 way of setting boolean attributes, as described here:

<一个href=\"http://stackoverflow.com/questions/4139786/what-does-it-mean-in-html-5-when-an-attribute-is-a-boolean-attribute\">What它意味着在HTML 5时属性是一个布尔属性?

在HTML 5,一个布尔值属性被设置是这样的:

In HTML 5, a bool attribute is set like this:

&LT;输入只读/&GT;

&LT;输入只读=只读/&GT;

所以剃刀视图引擎把你的模型的布尔值,将呈现(在我的情况),如果 Model.IsBoolValue 属性$ C>是真正。否则,如果它的那么属性完全不渲染。

So the Razor viewengine takes your model's bool value and will render (in my case) the value attribute if Model.IsBoolValue is true. Otherwise, if it's false then the value attribute is not rendered at all.

编辑:

由于在评论中提到Zabavsky,给力真或假的值出现在 attrbiute,简单的使用的ToString()

As mentioned Zabavsky in the comments, to force the value of True or False to appear in the value attrbiute, simple use ToString():

&LT;输入类型=隐藏值=@ Model.BoolProperty.ToString()/&GT;

这篇关于ASP.NET MVC 5呈现隐藏输入不同的bool值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆