C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET MVC Model BindingModel binding is a process in which we bind a model to controller and view. It is a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter. It acts as a converter because it can convert HTTP requests into objects that are passed to an action method. Example Here, we are creating an example, in which a simple model is binding with the view and controller. We are creating a Student model that has some properties. These properties will be used to create form fields. Create a ModelRight click on the Model folder and add a class to create new model. This file contains some default code, but we have added some properties to it. Model looks like this: // Student.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcApplicationDemo.Models { public class Student { public int ID { get; set; } public string Name { get; set; } public string Email { get; set; } public string Contact { get; set; } } } Create a ControllerAfter creating model, now let's create a controller for this class. Right click on the Controller folder and add the controller class. After adding, it provides the following predefined code. // StudentsController.csusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplicationDemo.Controllers { public class StudentsController : Controller { // GET: Students public ActionResult Index() { return View(); } } } Creating a ViewTo create view right click within the body of Index action method and select Add View option, it will pop up for the name of the view and Model to attach with the view. After adding, it generates an index file within the Students folder that contains the following code. // Index.cshtml@model MvcApplicationDemo.Models.Student @{ ViewBag.Title = "Index"; } <h2>Index</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Student</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> Output: When the Index file is executed it produces the following output.
Next TopicASP.NET MVC View
|