Entity Framework 4.1 Code First

I really like the new Entity Framework Code First. The best feature I like is the offloading the overhead  creation of Data Model or Database itself which EF takes care on its own. It also supports setting up initial seed data in the table too. Wow!!! This is the feature I have been longing for a while.

Starting up with EF 4.1  can be found here and resources found here.

Walkthru video can be found here.

In conjunction with Asp.net MVC3 EF Code First in EF 4.1 is the robust platform to develop Web applications.


How to set Html.RadioButtonFor as selected by default in Asp.net MVC3

The below is the code snippet of how to use the Html helper for Radio Button in Asp.net MVC.

<td> Will Accept?</td>

 <td>

@Html.RadioButtonFor(x => x.WillAccept, true)

@Html.RadioButtonFor(x => x.WillAccept, false)

</td>

 

Now how can we show one radio button selected by default? There are two ways of doing it.

The first option is to pass the “checked” html attribute as a parameter to the Html helper method by creating an anonymous object as below

<td> Will Accept?</td>

<td>

@Html.RadioButtonFor(x => x.WillAccept, true, new { @checked = “checked” })

@Html.RadioButtonFor(x => x.WillAccept, false)

</td>

The Second option is to set the value of the corresponding model property in the controller itself

public ActionResult Feedback()

{

UserResponse usr= new UserResponse();

usr.WillAccept = false;

return View(“Feedback”,usr);

}

Both work perfectly, but note that, setting the value via model overrides passing via html parameter. So if both of the above options are used the default value of the radio button will be false.

Leave a reply if you find it useful