0
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!

Konrad Gadzina
  • 3,407
  • 1
  • 18
  • 29
LooseLive
  • 69
  • 1
  • 8

1 Answers1

0

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.

Konrad Gadzina
  • 3,407
  • 1
  • 18
  • 29
  • Thanks, But I want a solution similar to the official. – LooseLive Feb 04 '13 at 00:30
  • 1
    What do you mean by the official solution? What's "not official" in using `Html.CheckBox` helper method or inline condition? Using helper method is the easiest way to do this. – Konrad Gadzina Feb 04 '13 at 00:33
  • Because I want to read the contents of a cshtml to match some things.For example, for each input box to add a "class" attribute. – LooseLive Feb 04 '13 at 00:45
  • 1
    @LooseLive Please update yout question with info about everything you want to do exactly. – Konrad Gadzina Feb 04 '13 at 00:58
  • @LooseLive, if you want help, you have to meet ppl at least half way. In this case you're asking Konrad to read your mind. When I get help, even if the person misses my intent, I am appreciative that someone is trying to help me. – Dave Alperovich Feb 04 '13 at 04:39