Monday, August 15, 2011

less abstraction and more Razor

I replaced this...

@{Html.RenderPartial("_MyPartial", Category.GetAll());}

 
 

...with this...

@Html.DropDownList("SampleCategory", new SelectList(Category.GetAll(), "Id", "Name"),

   "", new Dictionary<string, object>

   {

      {"class", "select01"}

   })

 
 

...in the name of LESS abstraction and MORE Razor. (Note above that the double quotes with nothing inside of them defines the first value of the dropdown list, a blank value.) The partial originally referenced looked like so:

@model IEnumerable<MyApp.Core.Entities.Category>

<select class="select01" name="SampleCategory">

   <option value="" selected></option>

   @foreach (Category category in Model)

   {

      <option value="@category.Id">@category.Name</option>

   }

</select>

 
 

Still getting used to Razor dropdown lists? Here is a good cheatsheet.

No comments:

Post a Comment