【发布时间】:2016-10-04 14:18:26
【问题描述】:
您好,我无法在多个帖子中保留值,并且我的控制器中的“Post1”操作函数始终将 MyViewModelObj.Field2 设为 null 。我希望它在第二个帖子中保留旧值 如何使 MyViewModel 模型类对象保持值?
Mymodels.cs(模型)
namespace RetainTest.Models
{
public class MyViewModel
{
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
}
}
RetainView.cshtml(视图)
@model RetainTest.Models.MyViewModel
@{
ViewBag.Title = "RetainView";
}
<h2>RetainView</h2>
@using (Html.BeginForm("Post1", "Retain", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Field1);
@Html.HiddenFor(model => model.Field2);
@Html.HiddenFor(model => model.Field3);
@Html.HiddenFor(model => model.Field4);
<div class="form-horizontal">
<h4>MyViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Field1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Field1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Field1, "", new { @class = "text-danger" })
</div>
</div>
@{
if ( Model.Field2 == "Val2")
{
<div class="form-group">
@Html.LabelFor(model => model.Field2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Field2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Field2, "", new { @class = "text-danger" })
</div>
</div>
}
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
RetainController.cs(控制器)
namespace RetainTest.Models
{
public class RetainController : Controller
{
// GET: Retain
public ActionResult Index()
{
MyViewModel MyViewModelObj = new MyViewModel();
MyViewModelObj.Field1 = "Val1";
return View("RetainView", MyViewModelObj);
}
[HttpPost]
public ActionResult Post1(MyViewModel MyViewModelObj)
{
if (string.IsNullOrEmpty(MyViewModelObj.Field2 ))
{
MyViewModelObj.Field2 = "Val2";
}
return View("RetainView", MyViewModelObj);
}
}
}
【问题讨论】:
-
您的视图对模型的每个属性都有一个隐藏的输入。
DefaultModelBinder读取请求中每个属性的第一个名称/值对并忽略所有其他属性。由于隐藏的输入是第一位的,因此您的EditorFor()方法的值将被忽略。您只绑定模型的初始值,而不是编辑的值(使您的表单毫无意义)。删除隐藏的输入! -
而
MyViewModelObj.Field2 = "Val2";将什么也不做(请参阅this answer 的第二部分以获得解释)。如果要显示具有不同值的视图,请遵循 PRG 模式(或使用ModelState.Clear();) -
感谢@StephenMuecke,删除这三个语句修复了它。 @Html.HiddenFor(model => model.Field1); @Html.HiddenFor(model => model.Field2); @Html.HiddenFor(model => model.Field3); @Html.HiddenFor(model => model.Field4);我虽然隐藏的价值是针对一篇文章,但我意识到它会永远保留它
标签: asp.net asp.net-mvc-4 razor