Please go through the steps i have shared. This a sample application for posting the person details to the server.
Step1: Create a Controller by Right clicking the Controller like below
public class PersonController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Create(Person person)
{
try
{
string name = person.Name;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
Step2 : Create a Model in the Model Folder like below
public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Step3:Create a View by Right Clicking the Index Action that i have mentioned in the controller and put the mark up in that view by removing the existing markups
@model MvcApplication1.Models.Person
@{
Layout = null;
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Create","Person")) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Id)
@Html.ValidationMessageFor(model => model.Id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
</body>
</html>
.
Step4:Avoid the Autoincrement property of Id field from the Database