{"id":4890,"date":"2024-02-06T08:04:34","date_gmt":"2024-02-06T08:04:34","guid":{"rendered":"https:\/\/isophal.com\/?p=4890"},"modified":"2024-02-06T08:04:35","modified_gmt":"2024-02-06T08:04:35","slug":"how-model-binding-works-in-asp-net-mvc-with-example","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2024\/02\/06\/4890.html\/","title":{"rendered":"How Model Binding Works In ASP.NET MVC With Example"},"content":{"rendered":"\n<p><em><strong>In this tutorial, you will learn:<\/strong><br><strong>1.<\/strong>&nbsp;What is Model Binding in MVC?<br><strong>2.<\/strong>&nbsp;How Model Binding Works<br><strong>3.<\/strong>&nbsp;Programming Example<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">WHAT IS MODEL BINDING IN MVC?<\/h3>\n\n\n\n<p>Do you ever wondered that how the form data gets automatically bound with model properties? Let\u2019s see the picture below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/model-binding.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/model-binding.png\" alt=\"model-binding\"\/><\/a><\/figure>\n\n\n\n<p>You have noticed the following thing:<\/p>\n\n\n\n<p><strong>1.<\/strong>&nbsp;It is much&nbsp;<strong>cleaner code<\/strong>&nbsp;and there is no fancy coding.<br><strong>2.<\/strong>&nbsp;No type casting needed.<br><strong>3.<\/strong>&nbsp;No need to remember model properties because it appears in IntelliSense.<br><strong>4.<\/strong>&nbsp;Adding and deleting properties from model is easy.<\/p>\n\n\n\n<p><strong>Model binding<\/strong>&nbsp;makes all these tasks easy. Simply, Model binding means bind your input control to the respective model properties and once it is bound, enjoy the power and simplicity of ASP.NET MVC. Model binding maps incoming data to corresponding model properties.<\/p>\n\n\n\n<p>In this tutorial, I will discuss several model binding techniques that will help you in access input data in models and controllers.<\/p>\n\n\n\n<p><strong>1.<\/strong>&nbsp;No Binding<br><strong>2.<\/strong>&nbsp;Simple Binding<br><strong>3.<\/strong>&nbsp;Class Binding<br><strong>4.<\/strong>&nbsp;Complex Binding<br><strong>5.<\/strong>&nbsp;FormCollection Binding<br><strong>6.<\/strong>&nbsp;Bind Attribute<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NO BINDING<\/h3>\n\n\n\n<p>No Binding means access form data directly without binding form to a specific model.<strong>Example:<\/strong><\/p>\n\n\n\n<p>Model: StudentModel.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>namespace MvcForms.Models<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class StudentModel<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public int Id { get; set; }<\/li>\n\n\n\n<li>public string Name { get; set; }<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p>View: Index.cshtml<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>&lt;h2&gt;Mvc Forms &#8211; Model Binding&lt;\/h2&gt;<\/li>\n\n\n\n<li>@using (Html.BeginForm())<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>&lt;table&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter ID: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBox(&#8220;Id&#8221;)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter Name: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBox(&#8220;Name&#8221;)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td colspan=&#8221;2&#8243;&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;\/table&gt;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&lt;h4 style=&#8221;color:purple&#8221;&gt;<\/li>\n\n\n\n<li>ID: @ViewBag.Id&lt;br \/&gt;<\/li>\n\n\n\n<li>Name: @ViewBag.Name&lt;br \/&gt;<\/li>\n\n\n\n<li>&lt;\/h4&gt;<\/li>\n<\/ol>\n\n\n\n<p>Controller: HomeController.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public ActionResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>if (Request.Form.Count &gt; 0)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.Id = Request.Form[&#8220;Id&#8221;];<\/li>\n\n\n\n<li>ViewBag.Name = Request.Form[&#8220;Name&#8221;];<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>return View();<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. SIMPLE BINDING<\/h3>\n\n\n\n<p>In simple binding, pass the parameter in action method with the same name as model properties and MVC Default Binder will automatically map correct action method for the incoming request.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/simple-model-binding.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/simple-model-binding.png\" alt=\"simple model binding\"\/><\/a><\/figure>\n\n\n\n<p>Controller: HomeController.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public ActionResult Index(int Id, string Name)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.Id = Id;<\/li>\n\n\n\n<li>ViewBag.Name = Name;<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">3. CLASS BINDING<\/h3>\n\n\n\n<p>In Class Binding, pass model as a parameter in action method and then access its entire member variable.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/New-Picture-3-3.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/New-Picture-3-3.png\" alt=\"Class Binding\"\/><\/a><\/figure>\n\n\n\n<p>Controller: HomeController.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>public ActionResult Index(StudentModel sm)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.Id = sm.Id;<\/li>\n\n\n\n<li>ViewBag.Name = sm.Name;<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4. COMPLEX BINDING<\/h3>\n\n\n\n<p><strong>Complex Binding<\/strong>&nbsp;means apply multiple related models in a single view. Here, in this example, I will show you how to bind multiple models in a single view.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong>&nbsp;Create two models class like that.<\/p>\n\n\n\n<p>Model: StudentModel.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>namespace MvcForms.Models<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class StudentModel<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public int Id { get; set; }<\/li>\n\n\n\n<li>public string Name { get; set; }<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>public CourseModel courseModel { get; set; }<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p>Model: CourseModel.cs<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>namespace MvcForms.Models<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class CourseModel<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public int Id { get; set; }<\/li>\n\n\n\n<li>public string Course { get; set; }<\/li>\n\n\n\n<li>public string Duration { get; set; }<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p>I have added&nbsp;<code>CourseModel<\/code>&nbsp;as a property in&nbsp;<code>StudentModel<\/code>, so the&nbsp;<code>StudentModel<\/code>&nbsp;has right to access&nbsp;<code>CourseModel<\/code>&nbsp;Property.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong>&nbsp;Create Form like that.<\/p>\n\n\n\n<p>View: Index.cshtml<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@model MvcForms.Models.StudentModel<\/li>\n\n\n\n<li>&lt;h2&gt;Mvc Forms &#8211; Model Binding&lt;\/h2&gt;<\/li>\n\n\n\n<li>@using (Html.BeginForm())<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>&lt;table&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter ID: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBoxFor(m =&gt; m.Id)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter Name: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBoxFor(m =&gt; m.Name)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter Course: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBoxFor(m =&gt; m.courseModel.Course)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td&gt;Enter Duration: &lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;td&gt;@Html.TextBoxFor(m =&gt; m.courseModel.Duration)&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;tr&gt;<\/li>\n\n\n\n<li>&lt;td colspan=&#8221;2&#8243;&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;&lt;\/td&gt;<\/li>\n\n\n\n<li>&lt;\/tr&gt;<\/li>\n\n\n\n<li>&lt;\/table&gt;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&lt;h4 style=&#8221;color:purple&#8221;&gt;<\/li>\n\n\n\n<li>ID: @ViewBag.Id&lt;br \/&gt;<\/li>\n\n\n\n<li>Name: @ViewBag.Name&lt;br \/&gt;<\/li>\n\n\n\n<li>Course: @ViewBag.Course&lt;br \/&gt;<\/li>\n\n\n\n<li>Duration: @ViewBag.Duration&lt;br \/&gt;<\/li>\n\n\n\n<li>&lt;\/h4&gt;<\/li>\n<\/ol>\n\n\n\n<p><strong>Step 3:<\/strong>&nbsp;Finally, the Action Method<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>[HttpGet]<\/li>\n\n\n\n<li>public ActionResult Index()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>return View();<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>public ActionResult Index(StudentModel sm)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewBag.Id = sm.Id;<\/li>\n\n\n\n<li>ViewBag.Name = sm.Name;<\/li>\n\n\n\n<li>ViewBag.Course = sm.courseModel.Course;<\/li>\n\n\n\n<li>ViewBag.Duration = sm.courseModel.Duration;<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/New-Picture-4-3.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/11\/New-Picture-4-3.png\" alt=\"Model Binding Output\"\/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SUMMARY<\/h3>\n\n\n\n<p>In this chapter, I discussed model binding in MVC with the help of an easy and simple example. However, there are also various ways by which you can bind views with models and controllers. Just read this chapter, If you are not comfortable in transferring data between models and views.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn:1.&nbsp;What is&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4891,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,188,140],"tags":[191],"class_list":["post-4890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net","category-c-sharp","category-programming","tag-asp-net"],"_links":{"self":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/comments?post=4890"}],"version-history":[{"count":1,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4890\/revisions"}],"predecessor-version":[{"id":4892,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4890\/revisions\/4892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4891"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}