在我为学习 MVC 所做的示例程序中,我有一个疑问。下面的代码是我的示例。
学生班
public class Student
{
public string Name { get; set; }
public string Age { get; set; }
public string Place { get; set; }
}
ViewOne
@model MyTestMVCApp.Models.Student
@{
ViewBag.Title = "ViewOne";
}
@using (Html.BeginForm())
{
<table style="border-color:Black;">
<tr>
<td>Name</td><td>Age</td><td>Place</td>
</tr>
</table>
<label>Enter Name : </label>
@Html.TextBoxFor(model => model.Name, new { name = "name"});
<input name="submit" type="submit" id="btnStart" class="button" value="Start Filling Details" />
}
ViewTwo.cshtml
@model MyTestMVCApp.Models.Student
@{
ViewBag.Title = "ViewTwo";
}
@using (Html.BeginForm("ViewTwo", "MyView"))
{
<table style="border-color:Black;">
<tr>
<td>Name</td><td>Age</td><td>Place</td>
</tr>
<tr>
<td>@Model.Name</td><td>@Model.Age</td><td>@Model.Place</td>
</tr>
</table>
<label>Enter Age : </label>
@Html.TextBoxFor(model => model.Age, new { name = "age" });
<input name="submit" type="submit" id="btnNext" class="button" value="Next" />
}
MyViewController.cs
public class MyViewController : Controller
{
public ActionResult ViewOne()
{
Student student = new Student();
return View(student);
}
[HttpPost]
public ActionResult ViewOne(Student student) // When comes here student contains value in name that I input.
{
return View("ViewTwo", student);
//return RedirectToAction("ViewTwo",student);
}
[HttpPost]
public ActionResult ViewTwo(Student student) // But here the name in student cleared and only age is there.
{
return View("ViewThree", student);
//return RedirectToAction("ViewThree", student);
}
}