数日前に MVC の勉強を始めたばかりです。私の知る限り、小さなサンプル プログラムを作成しました。しかし、私が直面していたいくつかの疑問。以下に疑問とコードを投稿しています。分かりやすく教えてください。
ここでは、4 つのビューとコントローラー、および学生クラスを作成しました。
学生クラス
public class Student
{
public string Name { get; set; }
public string Age { get; set; }
public string Place { get; set; }
}
ビューワン
@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>
<tr>
<td>--</td><td>--</td><td>--</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())
{
<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" />
}
ViewThree.cshtml
@model MyTestMVCApp.Models.Student
@{
ViewBag.Title = "ViewThree";
}
@using (Html.BeginForm())
{
<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.Place, new { name = "place" });
<input name="submit" type="submit" id="btnNext" class="button" value="Next" />
}
ViewFour.cshtml
@model MyTestMVCApp.Models.Student
@{
ViewBag.Title = "ViewFour";
}
@{
<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>
}
MyViewController.cs
public class MyViewController : Controller
{
public ActionResult ViewOne()
{
Student student = new Student();
return View(student);
}
[HttpPost]
public ActionResult ViewOne(Student student)
{
return View("ViewTwo", student);
//return RedirectToAction("ViewTwo",student);
}
[HttpPost]
public ActionResult ViewTwo(Student student)
{
return View("ViewThree", student);
//return RedirectToAction("ViewThree", student);
}
[HttpPost]
public ActionResult ViewThree(Student student)
{
return View("ViewFour", student);
}
}
私の疑問
疑い 1.ViewTwo のボタンをクリックすると、
[HttpPost]
public ActionResult ViewOne(Student student)
{
}
ViewTwo の [HttpPost] actionresult の代わりにデバッグ中です。なぜですか?
疑い 2.ViewOne で作成した学生オブジェクトの同じインスタンスを他のすべての Views に渡すにはどうすればよいですか?
ViewOne で生徒の name プロパティを取得し、同じオブジェクトを ViewTwo に渡します。
ViewTwo で学生の「年齢」プロパティを取得し、同じオブジェクトを ViewThree に渡します。
ViewThree で学生の「場所」プロパティを取得し、同じオブジェクトを ViewFour に渡します。
ViewFour では、上記のビューから取得した学生のすべての値を表示します。