asp.netasp.net-mvc-3razorviewresult

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

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

But in MVC 3,Need to write like this:

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? How to customize the upgrade code? Plase help me,thanks!


Solution

  • 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. 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:

    @{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.