1

在我为学习 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);
    }
 }

在此处输入图像描述

4

3 回答 3

3

将年龄和地点放入Hidden Field...

于 2013-07-04T03:51:48.157 回答
2

像这样修改你的视图二:

@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" />
@Html.HiddenFor(model => model.Name);
}

添加@Html.HiddenFor(model => model.Name);

在您的观点三中,您必须同时添加@Html.HiddenFor(model => model.Name);@Html.HiddenFor(model => model.Age);

于 2013-07-04T03:55:05.023 回答
1

@Html.Hidden返回一个包含隐藏字段的 IHtmlString。

@Html.HiddenFor(x=> x.Name);

或者

<input type='hidden' value='@Model.Name' name="Name" />

所以在你的代码中:

@model MyTestMVCApp.Models.Student
@{
ViewBag.Title = "ViewTwo";
}  
    @using (Html.BeginForm("ViewTwo", "MyView"))
    {         
     @Html.HiddenFor(x=> x.Name);
    <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" />
     }
于 2013-07-04T04:06:44.220 回答