Tuesday, 17 January 2012

MVC3 Razor - Basics

Creating Dropdownbox


Step 1: Create Data provider so that we can bind with drop down list



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace WebRepository
{
    public class UserProvider: IUserProvider
    {

        public List<SelectListItem> GetRoles()
        {
            List<SelectListItem> roles = new List<SelectListItem>();

            roles.Add(new SelectListItem() { Selected = false, Text = "Admin", Value = "0" });
            roles.Add(new SelectListItem() { Selected = true, Text = "Standard User", Value = "1" });
            roles.Add(new SelectListItem() { Selected = false, Text = "Super User", Value = "2" });

            return roles;
        }
    }
}



Step 2: Create action and fetch the data from data provider, bind with model and pass model to view.

        public ActionResult Index()
        {
            WebUser webUser = new WebUser();

            webUser.Name = "My Name";
            webUser.Role = new UserProvider().GetRoles();

            return View(webUser);
        }


Step 3:Pass model data to select list and bind with dropdown.



       @Html.DropDownList("UserRolesId", Model.Role)



Displaying Selected Value using JQuery


<script language="javascript">
    $('#UserRolesId').change(function () {
        $("option:selected").each(function () {
            alert($(this).text());
        });
    });
</script>



Attaches a change event to the select that gets the text for each selected option and writes them in the div. It then triggers the event for the initial text draw.



<!DOCTYPE html>
<html>
<head>
  <style>

  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <select name="sweets" multiple="multiple">
    <option>Chocolate</option>
    <option selected="selected">Candy</option>

    <option>Taffy</option>
    <option selected="selected">Caramel</option>
    <option>Fudge</option>
    <option>Cookie</option>

  </select>
  <div></div>
<script>
    $("select").change(function () {
          var str = "";
          $("select option:selected").each(function () {
                str += $(this).text() + " ";
              });
          $("div").text(str);
        })
        .change();
</script>

</body>
</html>



jQuery
==================
Async Call
-----------

http://forums.asp.net/t/1741929.aspx/1
http://api.jquery.com/jQuery.getJSON/


Entity Framework
==================

DB First
-----------

http://msdn.microsoft.com/en-us/data/jj206878.aspx


Simple Unit Of Work
--------------------

http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

Generic Unit Of Work
---------------------

http://blog.damianbrady.com.au/2012/07/24/a-generic-repository-and-unit-of-work-implementation-for-entity-framework/