In this article we will learn about Model Binding,Let's start.
First Add Model Class namely called Student in my case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Model_Binding.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
Now add Controller:
using Model_Binding.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Model_Binding.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
Student student = new Student() {
Id = 7401,
Name="Naveed Nayyar",
Gender="Male"
};
return View(student);
}
}
}
Now Add view:
@model Model_Binding.Models.Student
@{
ViewBag.Title = "Index";
}
<h2>Student Details</h2>
<table class="table-responsive table-hover">
<tr>
<td><b>ID</b></td>
<td>@Model.Id</td>
</tr>
<tr>
<td><b>Name</b></td>
<td>@Model.Name</td>
</tr>
<tr>
<td><b>Gender</b></td>
<td>@Model.Gender</td>
</tr>
</table>
When everything done build your project and run.
First Add Model Class namely called Student in my case.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Model_Binding.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}
Now add Controller:
using Model_Binding.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Model_Binding.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
Student student = new Student() {
Id = 7401,
Name="Naveed Nayyar",
Gender="Male"
};
return View(student);
}
}
}
Now Add view:
@model Model_Binding.Models.Student
@{
ViewBag.Title = "Index";
}
<h2>Student Details</h2>
<table class="table-responsive table-hover">
<tr>
<td><b>ID</b></td>
<td>@Model.Id</td>
</tr>
<tr>
<td><b>Name</b></td>
<td>@Model.Name</td>
</tr>
<tr>
<td><b>Gender</b></td>
<td>@Model.Gender</td>
</tr>
</table>
When everything done build your project and run.
Comments
Post a Comment