简体   繁体   English

如何更改ViewResult的内容

[英]How to change the content of ViewResult

bool isChecked = false;
<input type="checkbox" name="x" checked="@isChecked" />

In MVC 4, The above code will be generate as 在MVC 4中,以上代码将生成为

<input type="checkbox" name="x" />

But in MVC 3,Need to write like this: 但是在MVC 3中,需要这样写:

bool isChecked = false;
@if(isChecked)
{
   <input type="checkbox" name="x" checked="checked" /> 
}
else
{
    <input type="checkbox" name="x" /> 
}

If we are Microsoft developers, Which assembly need to modify and how to modify it? 如果我们是Microsoft开发人员,则需要修改哪个程序集以及如何对其进行修改? How to customize the upgrade code? 如何自定义升级代码? Plase help me,thanks! 请帮我,谢谢!

To be honest I don't really understand the question after those code blocks, but I can say that you can use inline condition in your views in ASP.NET MVC3. 坦白地说,在这些代码块之后,我并没有真正理解这个问题,但是我可以说,您可以在ASP.NET MVC3的视图中使用内联条件。 Something like that for example: 例如:

bool isChecked = false;
<input type="checkbox" name="x" @(isChecked ? "checked=checked" : "") />

It's shorter and it will produce code like that: 它更短,并且会生成如下代码:

<input type="checkbox" name="x">

And BTW, there is a helper method Html.CheckBox to create checkbox in your view and in second parameter you can indicate if you want it to be checked: 顺便说一句,有一个辅助方法Html.CheckBox可以在视图中创建复选框,在第二个参数中,您可以指示是否要对其进行检查:

@{bool isChecked = false;}    
@Html.CheckBox("x", isChecked)

And that will rendrer this: 这将使此问题:

<input id="x" type="checkbox" value="true" name="x">
<input type="hidden" value="false" name="x">

Try it on your own. 自己尝试。

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

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