{"id":4896,"date":"2024-02-06T12:28:56","date_gmt":"2024-02-06T12:28:56","guid":{"rendered":"https:\/\/isophal.com\/?p=4896"},"modified":"2024-02-06T12:28:58","modified_gmt":"2024-02-06T12:28:58","slug":"4-ways-to-get-form-data-in-asp-net-mvc-5","status":"publish","type":"post","link":"https:\/\/isophal.com\/news\/2024\/02\/06\/4896.html\/","title":{"rendered":"4 Ways To Get Form Data In ASP.NET MVC 5"},"content":{"rendered":"\n<p><strong>In this tutorial you will learn:<\/strong><br><strong>1.<\/strong>&nbsp;How to&nbsp;<strong>collect from value<\/strong>&nbsp;in Class in ASP.NET MVC 5 Project?<br><strong>2.<\/strong>&nbsp;What are the different ways to&nbsp;<strong>Retrieve Form Values<\/strong>?<br><strong>3.<\/strong>&nbsp;What is&nbsp;<strong><code>FormCollection<\/code><\/strong>&nbsp;Object,&nbsp;<strong><code>ModelBinder<\/code><\/strong>&nbsp;Class and&nbsp;<strong><code>UpdateModel<\/code><\/strong>&nbsp;Class?<\/p>\n\n\n\n<p>In this article, I am going to explain 4 different ways to Collect Form Data in Controller class in ASP.NET MVC 5. It is a very basic job for any application is to collect form data and process them with programming logic and save to the database. There are various ways to gather form value in class and I have added 4 most popular and suitable ways to do it.<\/p>\n\n\n\n<p><strong>1.\u00a0Create New ASP.NET MVC 5 Project and Add Controller, Models and Views.<\/strong><br><strong><strong>1.<\/strong>\u00a0Create New MVC Project<\/strong><code>SaveFormData<\/code>\u00a0in Visual Studio 2013,2015 or 2017. Go to\u00a0<strong>File<\/strong><strong>New<\/strong><strong>Project<\/strong>. Select\u00a0<strong>Web<\/strong>\u00a0in left pane and then select\u00a0<strong>ASP.NET Web Application (.Net Framework)<\/strong>\u00a0in middle panel. A Template window will be opened. Select\u00a0<strong>Empty<\/strong>\u00a0Template and Check\u00a0<strong>MVC CheckBox<\/strong>\u00a0and click\u00a0<strong>OK<\/strong>.<\/p>\n\n\n\n<p><strong><strong>2.<\/strong>&nbsp;Create Model:<\/strong>&nbsp;Create a Model class&nbsp;<code>StudentModel.cs<\/code>. Right click on&nbsp;<strong>Models<\/strong>&nbsp;Folder&nbsp;<strong>Add<\/strong><strong>Class<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/4.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/4.png\" alt=\"Add Model Class\"\/><\/a><\/figure>\n\n\n\n<p>Paste following code in&nbsp;<code>StudentModel.cs<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>namespace SaveFormData.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>public string City { get; set; }<\/li>\n\n\n\n<li>public string Address { get; set; }<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><strong>3.<\/strong>&nbsp;Create Controller:<\/strong>&nbsp;Create a Controller&nbsp;<code>HomeController.cs<\/code>.&nbsp;<strong>Right click<\/strong>&nbsp;on&nbsp;<strong>Controllers<\/strong>&nbsp;Folder &gt;&nbsp;<strong>Add<\/strong>&nbsp;&gt;&nbsp;<strong>Controller<\/strong>. Select&nbsp;<strong>MVC 5 Controller with read\/write actions<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/5.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/5.png\" alt=\"Add Controller\"\/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/6.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/6.png\" alt=\"Add Controller\"\/><\/a><\/figure>\n\n\n\n<p><strong><strong>4.<\/strong>&nbsp;Delete Unnecessary code<\/strong>&nbsp;and your&nbsp;<code>HomeController.cs<\/code>&nbsp;should look like this. However, it is not mandatory and you can use your own way to implement code.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using System.Web.Mvc;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>namespace SaveFormData.Controllers<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class HomeController : Controller<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/ GET: Home<\/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>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>public ActionResult Create()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>public ActionResult Create(FormCollection collection)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>try<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>catch<\/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>}<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><strong>5.<\/strong>&nbsp;Create Views:<\/strong><strong>Right click<\/strong>&nbsp;on&nbsp;<code>Create()<\/code>&nbsp;Action Method in&nbsp;<strong>HomeController<\/strong>&nbsp;class and select&nbsp;<strong>Add View<\/strong>. Give View Name&nbsp;<strong>Create<\/strong>&nbsp;and then choose&nbsp;<strong>Create<\/strong>&nbsp;Template from dropdown list. Select&nbsp;<strong>StudentModel<\/strong>&nbsp;from Model Class dropdown list.&nbsp;<strong>Check Use a layout page<\/strong>&nbsp;and click&nbsp;<strong>Add<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/2-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/2-1.png\" alt=\"Add View with Model\"\/><\/a><\/figure>\n\n\n\n<p><strong>Create.cshtml View Page<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@model SaveFormData.Models.StudentModel<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>@{<\/li>\n\n\n\n<li>ViewBag.Title = &#8220;Create&#8221;;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;h2&gt;Create&lt;\/h2&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>@using (Html.BeginForm())<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>@Html.AntiForgeryToken()<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>&lt;div class=&#8221;form-horizontal&#8221;&gt;<\/li>\n\n\n\n<li>&lt;h4&gt;StudentModel&lt;\/h4&gt;<\/li>\n\n\n\n<li>&lt;hr \/&gt;<\/li>\n\n\n\n<li>@Html.ValidationSummary(true, &#8220;&#8221;, new { @class = &#8220;text-danger&#8221; })<\/li>\n\n\n\n<li>&lt;div class=&#8221;form-group&#8221;&gt;<\/li>\n\n\n\n<li>@Html.LabelFor(model =&gt; model.Name, htmlAttributes: new { @class = &#8220;control-label col-md-2&#8221; })<\/li>\n\n\n\n<li>&lt;div class=&#8221;col-md-10&#8243;&gt;<\/li>\n\n\n\n<li>@Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = &#8220;form-control&#8221; } })<\/li>\n\n\n\n<li>@Html.ValidationMessageFor(model =&gt; model.Name, &#8220;&#8221;, new { @class = &#8220;text-danger&#8221; })<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;div class=&#8221;form-group&#8221;&gt;<\/li>\n\n\n\n<li>@Html.LabelFor(model =&gt; model.City, htmlAttributes: new { @class = &#8220;control-label col-md-2&#8221; })<\/li>\n\n\n\n<li>&lt;div class=&#8221;col-md-10&#8243;&gt;<\/li>\n\n\n\n<li>@Html.EditorFor(model =&gt; model.City, new { htmlAttributes = new { @class = &#8220;form-control&#8221; } })<\/li>\n\n\n\n<li>@Html.ValidationMessageFor(model =&gt; model.City, &#8220;&#8221;, new { @class = &#8220;text-danger&#8221; })<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;div class=&#8221;form-group&#8221;&gt;<\/li>\n\n\n\n<li>@Html.LabelFor(model =&gt; model.Address, htmlAttributes: new { @class = &#8220;control-label col-md-2&#8221; })<\/li>\n\n\n\n<li>&lt;div class=&#8221;col-md-10&#8243;&gt;<\/li>\n\n\n\n<li>@Html.EditorFor(model =&gt; model.Address, new { htmlAttributes = new { @class = &#8220;form-control&#8221; } })<\/li>\n\n\n\n<li>@Html.ValidationMessageFor(model =&gt; model.Address, &#8220;&#8221;, new { @class = &#8220;text-danger&#8221; })<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;div class=&#8221;form-group&#8221;&gt;<\/li>\n\n\n\n<li>&lt;div class=&#8221;col-md-offset-2 col-md-10&#8243;&gt;<\/li>\n\n\n\n<li>&lt;input type=&#8221;submit&#8221; value=&#8221;Create&#8221; class=&#8221;btn btn-default&#8221; \/&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;div&gt;<\/li>\n\n\n\n<li>@Html.ActionLink(&#8220;Back to List&#8221;, &#8220;Index&#8221;)<\/li>\n\n\n\n<li>&lt;\/div&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;script src=&#8221;~\/Scripts\/jquery-1.10.2.min.js&#8221;&gt;&lt;\/script&gt;<\/li>\n\n\n\n<li>&lt;script src=&#8221;~\/Scripts\/jquery.validate.min.js&#8221;&gt;&lt;\/script&gt;<\/li>\n\n\n\n<li>&lt;script src=&#8221;~\/Scripts\/jquery.validate.unobtrusive.min.js&#8221;&gt;&lt;\/script&gt;<\/li>\n<\/ol>\n\n\n\n<p><strong>6.<\/strong>&nbsp;Using same process create Index view Page. Right&nbsp;<strong>click<\/strong>&nbsp;on&nbsp;<strong>Index<\/strong>&nbsp;Action Method in&nbsp;<strong>HomeController<\/strong>&nbsp;class and click&nbsp;<strong>Add View<\/strong>. Give View Name:&nbsp;<strong>Index<\/strong>, Choose Template:&nbsp;<strong>Empty (Without Model)<\/strong>, tick on&nbsp;<strong>Use a Layout Page<\/strong>&nbsp;and click on&nbsp;<strong>Add<\/strong>&nbsp;button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/18.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/18.png\" alt=\"Add View from Model\"\/><\/a><\/figure>\n\n\n\n<p><strong>Index.cshtml View Page<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>@{<\/li>\n\n\n\n<li>ViewBag.Title = &#8220;Index Page&#8221;;<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;h2&gt;View Details&lt;\/h2&gt;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>&lt;strong&gt;Name&lt;\/strong&gt; : @ViewData[&#8220;Name&#8221;]&lt;br \/&gt;<\/li>\n\n\n\n<li>&lt;strong&gt;City&lt;\/strong&gt; : @ViewData[&#8220;City&#8221;]&lt;br \/&gt;<\/li>\n\n\n\n<li>&lt;strong&gt;Address&lt;\/strong&gt; : @ViewData[&#8220;Address&#8221;]&lt;br \/&gt;<\/li>\n<\/ol>\n\n\n\n<p>Now, your project is ready with a form Create. You can navigate it like this<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/3-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/3-1.png\" alt=\"Create page\"\/><\/a><\/figure>\n\n\n\n<p>In this article, I will fill some data in Create Form and when click Create Button, it will display Index View Page with Filled data.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a>&nbsp;<\/a><\/p>\n\n\n\n<p><strong>Method 1.&nbsp;Retrieve Form Data using FormCollection Object.<\/strong><\/p>\n\n\n\n<p><code>FormCollection<\/code>&nbsp;object is used for accessing Form Data in controller class. The&nbsp;<strong>FormCollection<\/strong>&nbsp;Object automatically receives the posted form data. In the following example I am going to explain how to use&nbsp;<strong>FormCollection<\/strong>&nbsp;Object to retrieve form data into controller class.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using System.Web.Mvc;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>namespace SaveFormData.Controllers<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class HomeController : Controller<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/ GET: Home<\/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>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>public ActionResult Create()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>public ActionResult Create(FormCollection collection)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>try<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/Method 1: Using Component Name<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>\/*ViewData[&#8220;Name&#8221;] = collection[&#8220;Name&#8221;];<\/li>\n\n\n\n<li>ViewData[&#8220;City&#8221;] = collection[&#8220;City&#8221;];<\/li>\n\n\n\n<li>ViewData[&#8220;Address&#8221;] = collection[&#8220;Address&#8221;];*\/<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>\/\/Method 2: Using Component Index Position<\/li>\n\n\n\n<li>ViewData[&#8220;Name&#8221;] = collection[1];<\/li>\n\n\n\n<li>ViewData[&#8220;City&#8221;] = collection[2];<\/li>\n\n\n\n<li>ViewData[&#8220;Address&#8221;] = collection[3];<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>catch<\/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>}<\/li>\n\n\n\n<li>}<\/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\/03\/4-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/4-1.png\" alt=\"Output\"\/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/5-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/5-1.png\" alt=\"Output\"\/><\/a><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a>&nbsp;<\/a><\/p>\n\n\n\n<p><strong>Method 2.&nbsp;Retrieve Form Data using Model Binder &#8211; Using Parameter Type.<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using System.Web.Mvc;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>namespace SaveFormData.Controllers<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class HomeController : Controller<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/ GET: Home<\/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>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>public ActionResult Create()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>public ActionResult Create(string name, string city, string address)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>try<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewData[&#8220;Name&#8221;] = name;<\/li>\n\n\n\n<li>ViewData[&#8220;City&#8221;] = city;<\/li>\n\n\n\n<li>ViewData[&#8220;Address&#8221;] = address;<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>catch<\/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>}<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p>https:\/\/googleads.g.doubleclick.net\/pagead\/ads?gdpr=0&amp;us_privacy=1&#8212;&amp;gpp_sid=-1&amp;client=ca-pub-2586160620136296&amp;output=html&amp;h=200&amp;slotname=3653952594&amp;adk=3083015738&amp;adf=1523998013&amp;pi=t.ma~as.3653952594&amp;w=1021&amp;fwrn=4&amp;lmt=1707222333&amp;rafmt=11&amp;format=1021&#215;200&amp;url=https%3A%2F%2Fwww.completecsharptutorial.com%2Fmvc-articles%2F4-ways-to-collect-form-data-in-controller-class-in-asp-net-mvc-5.php&amp;wgl=1&amp;uach=WyJXaW5kb3dzIiwiMTAuMC4wIiwieDg2IiwiIiwiMTIxLjAuMjI3Ny45OCIsbnVsbCwwLG51bGwsIjY0IixbWyJOb3QgQShCcmFuZCIsIjk5LjAuMC4wIl0sWyJNaWNyb3NvZnQgRWRnZSIsIjEyMS4wLjIyNzcuOTgiXSxbIkNocm9taXVtIiwiMTIxLjAuNjE2Ny4xMzkiXV0sMF0.&amp;dt=1707222313190&amp;bpp=1&amp;bdt=704&amp;idt=334&amp;shv=r20240201&amp;mjsv=m202401300101&amp;ptt=9&amp;saldr=aa&amp;abxe=1&amp;cookie=ID%3Dda923ce1809fdb65%3AT%3D1707206552%3ART%3D1707222311%3AS%3DALNI_MaK2d3SPUVF4IJTxgRvNqlZhBffOw&amp;gpic=UID%3D00000cfa5e8dcaef%3AT%3D1707206552%3ART%3D1707222311%3AS%3DALNI_MbTGMvXV3ObqK2p_zeWAx2rZgcV7w&amp;eo_id_str=ID%3Dc11bec4f34fbef8f%3AT%3D1707206552%3ART%3D1707222311%3AS%3DAA-AfjZVxZpALXO5F2y5gGs4A3md&amp;prev_fmts=0x0%2C1021x200%2C300x600%2C1021x280&amp;nras=2&amp;correlator=7237666580002&amp;frm=20&amp;pv=1&amp;ga_vid=1424845412.1707206554&amp;ga_sid=1707222313&amp;ga_hid=2092292844&amp;ga_fc=1&amp;rplot=4&amp;u_tz=420&amp;u_his=1&amp;u_h=1080&amp;u_w=1920&amp;u_ah=1040&amp;u_aw=1920&amp;u_cd=24&amp;u_sd=1&amp;dmc=8&amp;adx=281&amp;ady=11234&amp;biw=1857&amp;bih=966&amp;scr_x=0&amp;scr_y=7392&amp;eid=44759876%2C44759927%2C44759837%2C44809530%2C95322434%2C31080836%2C95324155%2C95324160&amp;oid=2&amp;pvsid=1765846021818514&amp;tmod=131698149&amp;wsm=1&amp;uas=0&amp;nvt=1&amp;ref=https%3A%2F%2Fwww.google.com%2F&amp;fc=1920&amp;brdim=0%2C0%2C0%2C0%2C1920%2C0%2C1920%2C1040%2C1872%2C966&amp;vis=1&amp;rsz=%7C%7CEebr%7C&amp;abl=CS&amp;pfx=0&amp;fu=128&amp;bc=31&amp;bz=1.03&amp;psd=W251bGwsbnVsbCxudWxsLDNd&amp;ifi=3&amp;uci=a!3&amp;btvi=2&amp;fsb=1&amp;dtd=20500<strong>Explanation:<\/strong><\/p>\n\n\n\n<p>I know what is going on your mind. You are thinking that how MVC map parameter exactly with correct components. It is very simple. MVC has a&nbsp;<strong>Model Binder<\/strong>&nbsp;that does the following task to match parameter with control.<\/p>\n\n\n\n<p><strong>a.<\/strong>&nbsp;Go to View Page<br><strong>b.<\/strong>&nbsp;Look for control name that match with parameter name.<br><strong>c.<\/strong>&nbsp;Map that control to parameter with same name.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/6-1.png\"><img decoding=\"async\" src=\"https:\/\/www.completecsharptutorial.com\/wp-content\/uploads\/2017\/03\/6-1.png\" alt=\"Model Binder Example\"\/><\/a><\/figure>\n\n\n\n<p>But here is a catch. You have to use the same name as your component name. If you look for Create.cshtml source page you will notice that the controls name are same as the parameter name. It doesn&#8217;t matter ordering of parameter but naming should be same.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a>&nbsp;<\/a><\/p>\n\n\n\n<p><strong>Method 3.&nbsp;Retrieve Form Data Directly from the Model Class<\/strong><strong>Receive Form Data using StudentModel class<\/strong><\/p>\n\n\n\n<p>In previous method,&nbsp;<strong>Model Binder<\/strong>&nbsp;works great if you have a form with 3 or 5 controls but what if you have a form with 30+ controls. It is not practical to use a 30+ parameter to retrieve form data. Your Model class fixes these issues and makes this job very easier. How see the example.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>You must import SaveFormData.Models to use StudentModel Object<\/strong><\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using System.Web.Mvc;<\/li>\n\n\n\n<li>using SaveFormData.Models;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>namespace SaveFormData.Controllers<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class HomeController : Controller<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/ GET: Home<\/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>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>public ActionResult Create()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>public ActionResult Create(StudentModel smodel)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>try<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>if (ModelState.IsValid)<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>ViewData[&#8220;Name&#8221;] = smodel.Name;<\/li>\n\n\n\n<li>ViewData[&#8220;City&#8221;] = smodel.City;<\/li>\n\n\n\n<li>ViewData[&#8220;Address&#8221;] = smodel.Address;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>else<\/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>}<\/li>\n\n\n\n<li>catch<\/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>}<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<p>In this example, you can receive data directly from your model class.&nbsp;<code>ModelState.IsValid<\/code>&nbsp;properties ensure that all the data filled in a control was in a correct format and validated.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a>&nbsp;<\/a><\/p>\n\n\n\n<p><strong>Method 4.&nbsp;Retrieve Form Data using UpdateModel class<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>You must import SaveFormData.Models to use StudentModel Object<\/strong><\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>using System.Web.Mvc;<\/li>\n\n\n\n<li>using SaveFormData.Models;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>namespace SaveFormData.Controllers<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>public class HomeController : Controller<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\/\/ GET: Home<\/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>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>[HttpGet]<\/li>\n\n\n\n<li>[ActionName(&#8220;Create&#8221;)]<\/li>\n\n\n\n<li>public ActionResult Create_get()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>[ActionName(&#8220;Create&#8221;)]<\/li>\n\n\n\n<li>public ActionResult Create_post()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>try<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>StudentModel smodel = new StudentModel();<\/li>\n\n\n\n<li>UpdateModel(smodel);<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>ViewData[&#8220;Name&#8221;] = smodel.Name;<\/li>\n\n\n\n<li>ViewData[&#8220;City&#8221;] = smodel.City;<\/li>\n\n\n\n<li>ViewData[&#8220;Address&#8221;] = smodel.Address;<\/li>\n\n\n\n<li>&nbsp;<\/li>\n\n\n\n<li>return View(&#8220;Index&#8221;);<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>catch<\/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>}<\/li>\n\n\n\n<li>}<\/li>\n\n\n\n<li>}<\/li>\n<\/ol>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>The&nbsp;<strong>UpdateModel<\/strong>&nbsp;class&nbsp;<strong>binds control data to models property<\/strong>. But there is a catch. When you use&nbsp;<strong>UpdateModel<\/strong>&nbsp;class, there is no parameter in&nbsp;<code>[HttpPost] Create()<\/code>&nbsp;Method. As you know that there can not be two methods in a class with same name and same parameter types. As there are already a&nbsp;<code>[HttpGet] Create()<\/code>&nbsp;method without parameter, so you cannot declare&nbsp;<code>[HttpPost] Create()<\/code>&nbsp;method without parameter. So the solution is to change Create Method name as&nbsp;<code>Create_Get()<\/code>&nbsp;and&nbsp;<code>Create_Post()<\/code>&nbsp;or whatever you want to change. Then decorate both method with correct action name as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\/\/ GET: Home\/Create<\/li>\n\n\n\n<li>[HttpGet]<\/li>\n\n\n\n<li>[ActionName(&#8220;Create&#8221;)]<\/li>\n\n\n\n<li>public ActionResult Create_get()<\/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>\/\/ POST: Home\/Create<\/li>\n\n\n\n<li>[HttpPost]<\/li>\n\n\n\n<li>[ActionName(&#8220;Create&#8221;)]<\/li>\n\n\n\n<li>public ActionResult Create_post()<\/li>\n\n\n\n<li>{<\/li>\n\n\n\n<li>\u2026\u2026..<\/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\">SUMMARY<\/h3>\n\n\n\n<p>In this article, I have shown you&nbsp;<strong>4 different ways to retrieve form value in controller class<\/strong>. It is very necessary to know how to collect form data in a class for each MVC Programmer or learner. Hope, your doubt would be cleared.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial you will learn:1.&nbsp;How to&nbsp;collect&hellip;<\/p>\n","protected":false},"author":1,"featured_media":4897,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,188,140],"tags":[191],"class_list":["post-4896","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\/4896","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=4896"}],"version-history":[{"count":1,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4896\/revisions"}],"predecessor-version":[{"id":4898,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/posts\/4896\/revisions\/4898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media\/4897"}],"wp:attachment":[{"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/media?parent=4896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/categories?post=4896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isophal.com\/news\/wp-json\/wp\/v2\/tags?post=4896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}