ASP.NET MVC , Razor and Validations!!

Just created a sample to showcase EF 4.0 Code First approach..it so happens that the app made to showcase uses the MVC 3.0 pattern with Razor implementation .

So why not highlight a few points there too.

  1. Goodbye <%=%> , hello @
    @Model.IdeaTitle +":"+@Model.Description+" :"+ @Model.SubmittedBy 

  1. Server tag commenting…
    @*@{
        ViewBag.Title = "Index";
    }*@

  2. ViewBag plus View
     public ActionResult Index()
    {
       IdeaContext context= new IdeaContext();
        var j = context.Ideas;
        ViewBag.Title = " Idea List";
        return View(j.ToList());
     }

@model IEnumerable<IdeaMVC.Models.IdeaModel>

<h2>@ViewBag.Title</h2>

4.   User Controls..

@Html.Partial("_DetailsPartialPage", Model)

5. Server side validation using Annotations (Server Side)

 [HttpPost]
 public ActionResult Create(IdeaModel  idea)
 {
   try
     {
        if (ModelState.IsValid)
         {...
         }
    }
}

6. Client Side Validatation

<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type ="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type ="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcValidation.js")" type ="text/javascript"></script>

image

7. Layout..

  <div id="main">
            @RenderBody()
            <div id="footer">
            </div>
   </div>

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Small snippets to showcase changes…There are of course many more changes/improvements but then the intention was to showcase Code First Entity Framework…a Full Fledged MVC 3.0 coming up soon..

Incase you want the full app for download (this plus Code First EF) do drop me a mail..

Cennest

Advertisements