Sunday, 27 March 2011

Asp.net mvc 3 built in json model binding

One of the things in mvc3 that recently caught my eyes is built in json model binding support which I have recently used in my project and want to write about it today. JsonValueProviderFactory Class which do all the stuff regarding json model binding is now built-in in mvc 3 which was not in mvc2. To use it in mvc2 you have to register the JsonValueProviderFactory class in global.asax after referencing Microsoft.Web.Mvc from Mvc Futures Library . Here how the thing goes in mvc3

Model Class

  public class Category
  { 
     public string CategoryName { get; set; }
  }
The Controller
  public ActionResult Create()
  {   
     return View();
  }
   
  [HttpPost]
  public ActionResult Create(Category category )
  {
     string Cat = category.CategoryName; 
     var data = new { message = "ok" };
     return Json(data); 
  }

The View
$(document).ready(function () { 
    $('#save').click(function () {

        var CategoryName = { CategoryName: $('#CategoryName').val() };

        $.ajax({
            url: "/Category/Create",
            data: JSON.stringify(CategoryName),
            contentType: "application/json; charset=utf-8",
            success: function (mydata) {
                $("#message").html(mydata["message"]);
            },         
            type: "POST",
            datatype: "json"
        });
        return false;
    });
}); 

@using (Html.BeginForm()) { @Html.ValidationSummary(true)
@Html.EditorFor(model => model.CategoryName) @Html.ValidationMessageFor(model => model.CategoryName)
}

For Model binding to work in Mvc2 Add JsonValueProviderFactory
in Global.asax after referencing Microsoft.Web.Mvc
   protected void Application_Start() 
   {
     RegisterRoutes(RouteTable.Routes);
     ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
   }

Thursday, 24 March 2011

My Asp.net mvc,Asp.net, linq and jquery Faqs

Here I want to make the list of every thing about Asp.mvc, linq and jquery I have been involved in or I can get my hands on or find very interesting. The list will keep on growing over time.

How to get the current Route Id in the view from Url?

<%=Url.RequestContext.RouteData.Values["id"] %> 

What is difference between Html.Partial and Html.RenderPartial?

Good complete answer here.

What is Mvc Futures Library?

Please refer here for the answer.
http://stackoverflow.com/questions/2734316/asp-net-mvc-futures-refresh-for-mvc2.

What is TempData?

Answer here

What is Difference between ViewData and ViewBag?

See here And here

Many to Many Relationship in linq to sql ?

http://geekswithblogs.net/WinAZ/archive/2010/01/09/simplified-many-to-many-relationships-with-linq-to-sql.aspx
http://www.codeproject.com/KB/linq/linq-to-sql-many-to-many.aspx
http://www.iaingalloway.com/many-to-many-relationships-in-linq-to-sql-part-2

How to catch error in Jquery's load ?
To Invoke It: Call throw new Exception() from controller's catch
 
var ph = $("#Cat");
            ph.load("/Categories/Index", function (responseText, textStatus, req) {
                if (textStatus == "error") {
                    window.location = "../User/Logon";
                }
                else {
                alert(textstatus);
                }
            });

 
How to Create comma seperated string using linq?
 var Tags = _db.TagPosts.Where(m => m.bint_PostId == id);
 return string.Join(",", (from p in Tags select p.Tag.vcr_TagName)); 

Linq to sql case insensitive string matching?

if (string.Equals(string1,string2,StringComparison.OrdinalIgnoreCase)) 
{ 
}


Exporting Gridview DataSet to Excel


http://stackoverflow.com/questions/5621577/export-from-sql-server-to-excel-file-using-asp-net-and-vb-net

Get the text and value of the selected item of dropdown in jquery
alert($("#selectlist option:selected").text());
alert($("#selectlist option:selected").val());

Find out If a particular element exists in jquery
if ($("#row-" + exist).length) {

  }