This article requires a basic understanding of MVC architecture. If you are very new to MVC then I suggest you continue with this article after getting the basic idea of MVC design pattern.
In this article we will learn how to bind a Model with a view in the MVC architecture. We know that a Model is nothing but our business class and a view presents the model data to the user. Cool, let’s think that we have defined one model to register the user’s information.
And depending on that we have designed the view to get all the information. Then how will we pass the entire view data to the controller?
This is were the Model binding is relevant. If we bind the model with the view then the MVC engine will automatically collect all the model properties from the view field. Let’s implement the concept with a small example.
At first we will create a Model as in the following.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MVC.Models
{
public class person
{
public string _name{get;set;}
public string _surname { get; set; }
public int _age { get; set; }
}
}
The model is extremely simple, it contains only three fields. And please keep in mind that from the view we will collect data only for those three fields.
Here is my view
Please see that I have created a strongly typed view though it is not necessary. In the body:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC.Models.person>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>person</title>
</head>
<body>
<div>
<% using (Html.BeginForm("SavePerson","person")){ %>
Name : <%= Html.TextBox("_name") %> <br />
Surname: <%= Html.TextBox("_surname") %> <br />
Age: <%= Html.TextBox("_age") %><br />
<input value="Register" type="submit" />
<% } %>
</div>
</body>
</html>
Part we have defined the form element. And in the BeginForm() helper method we have specified two parameters, the first one indicating the action name and the second one is the controller name.
Once we click on the submit button, it will hit the targeted controller.
We will now define the controller to collect form data. Have a look at the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
namespace MVC.Controllers
{
public class personController : Controller
{
public ActionResult AddNew()
{
return View("person");
}
public ActionResult SavePerson([Bind] person p)
{
return Content("Data Send");
}
}
}
The the person controller contains two actions. The AddNew() action will return a “person” view that is nothing but our previous HTML form.
And the savePerson is the targeted controller on the form’s submitting operation. Now if we run the application and a browse/person/AddNew URL then we will find the following view.
I have provided data to my view and once I submit it, it will hit the SavePerson() action. And we are getting all form data as a parameter of the SavePerson() action.
ConclusionIn this article we saw how to send data from a view to a controller using a Model binding technique. I hope you have understood it and it will help you in your day to day programmer life.