对象中的值被清除

时间:2013-07-04 03:49:07

标签: c# asp.net-mvc-3 razor

在我研究MVC的示例程序中,我有一个疑问。下面的代码是我的样本。

StudentClass

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);
    }
 }

enter image description here

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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);

答案 2 :(得分: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" />
     }