Tutorial by Examples

Given some default routing such as {controller=Home}/{action=Index}/{id?} if you had the url https://stackoverflow.com/questions/1558902 This would go to the QuestionsController and the value 1558902 would be mapped to an id parameter of an index action, i.e. public ActionResult Index(int? id){ ...
To extend on the route binding say you had a url like https://stackoverflow.com/questions/1558902?sort=desc and routing like {controller=Home}/{action=Index}/{id?} public ActionResult Index(int? id, string sort){ //sort would bind to the value in the query string, i.e. "desc" } ...
Often you'd be working with viewmodel classes in asp.net-mvc and would want to bind to properties on these. This works similar to mapping to individual parameters. Say you had a simple view model call PostViewModel like this public class PostViewModel{ public int Id {get;set;} public int Sn...
These are form values that go in the HTTP request using the POST method. (including jQuery POST requests). Say you did an ajax post like $.ajax({ type: 'POST', url: window.updatePost, data: { id: 21, title: 'snappy title' }, //kept short for clarity }); Here the two values...
Sometimes we need preserve whole model and transfer it across actions or even controllers. Storing model at session good solution for this type of requirements. If we combine this with powerful model binding features of MVC we get elegant way of doing so. We can create generic session based model bi...
Considering a (post)model: public class User { public string FirstName { get; set; } public bool IsAdmin { get; set; } } With a view like so: @using (Html.BeginForm()) { @Html.EditorFor(model => model.FirstName) <input type="submit" value="Save" /...
Model: public class SampleViewModel { public HttpPostedFileBase file {get;set;} } View: @model HelloWorldMvcApp.SampleViewModel @using (Html.BeginForm("Index","Home",FormMethod.Post, new { enctype = "multipart/form-data" })) { <div class="fo...
If different users need different datetime format then you may need to parse your incoming date string to actual date according to the format. In this case this snippet may help you. public class DateTimeBinder : DefaultModelBinder { public override object BindModel(ControllerContext control...

Page 1 of 1