22 July 2013

Cheats : Razor time

This is not a tutorial. I provide this short cheat for my small team that develops small "Kampung Project" in MVC.

"Just pick-up things that you need. Nobody needs all the things. Only one thing is necessary."

Title
@{ViewBag.Title = "Kampung Project"}

To use: <h1> @ViewBag.Title </h1>

TextBox
@Html.EditorFor(model => model.Name)

Label
@Html.LabelFor(model => model.LastName)

Validation Message
@Html.ValidationMessageFor(model => model.Name)

Validation Summary
@Html.ValidationSummary(true)

List
<ul>
@foreach (var dept in Model)
  {
    <li> @dept.Name </li>
  }
</ul>

HyperLink
<li>
  @Html.ActionLink("Click Here", "Index", "Home")
</li>

Other HyperLink samples
@Html.ActionLink(department.Name, "Index", "Home", new{department.Id}, null)
@Html.ActionLink(department.Name, "Index", "Home", new{Id = @Model.Id}, null)

Table Sample
<table>
  <tr>
    <th> Name List </th>
  </tr>

@foreach(var dept in Model)
  {
    <tr>
      <td> @dept.Name </td>
      <td> .....................</td>
   </tr>
  }
</table>

DropDownList Sample
@Html.DropDownListFor(model => model.Category, (SelectList) ViewBag.CategoryList)

Then add this in controller,

var category = new SelectList (new[] { "Sales", "Admin" } );

ViewBag.CategoryList = category;


 

No comments:

Post a Comment