【发布时间】:2013-07-24 14:07:01
【问题描述】:
我昨天问过问题,可以在here 和
根据我从 here 的 asp.net/mvc 论坛获得的答案,我被告知要清除我的模型状态,因为默认情况下我的表单倾向于保持其默认值,而不是我刚刚尝试更新的值.所以,我加了Modelstate.Clear(),还是不行。谁能告诉我我是否在错误的地方使用了 ModelState.Clear() 或者我是否需要更改某些内容?
所以,问题来了,我有一个编辑表单,当用户单击编辑按钮时,它会在文本框中显示其当前值。如果用户想要编辑文本框中显示的某个当前值,他编辑文本框中的值并单击保存更改按钮。当前正在发生的事情是在我的 HttpPost 方法中,当我检查正在传递的值时,我没有得到用户刚刚提供的新值,而是得到了在表单中显示为当前值的值。 但是当我在 chrome 中签入开发人员工具时,它会显示刚刚提供的新值 user 作为正在传递给服务器的值。
这是我的看法
@using BootstrapSupport
@model AdminPortal.Areas.Hardware.Models.EditModule
@{
ViewBag.Title = "Edit";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
<fieldset>
<legend>Module <small>Edit</small></legend>
@using (Html.BeginForm("Edit", "Module"))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(m=>m.Id)
for(var i = 0; i < Model.Properties.Count(); i++)
{
@Html.HiddenFor(model=>model.HiddenProperties[i].Name)
@Html.HiddenFor(model=>model.HiddenProperties[i].Value)
<label class="label">@Model.Properties[i].Name</label>
<div class="input-block-level">@Html.TextBoxFor(model => model.Properties[i].Value)</div>
}
<div class="form-actions">
<button type="submit" class="btn btn-primary" id="Submit">Save changes</button>
@Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " })
</div>
}
</fieldset>
<p>
@Html.ActionLink("Back to List", "ModuleList")
</p>
这是控制器中的get和post方法
[HttpGet]
public ActionResult Edit(long id)
{
var module = _repository.GetModuleProperties(id);
ModelState.Clear();
return View(module);
}
[HttpPost]
public ActionResult Edit(EditModule module)
{
ModelState.Clear();
if (ModelState.IsValid)
{
_repository.SaveModuleEdits(module);
Information("Module was successfully edited!");
return RedirectToAction("ModuleList", "Module", new {area = "Hardware"});
}
Error("Edit was unsuccessful, if the problem persists please contact Merijn!");
return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });
}
【问题讨论】:
标签: asp.net-mvc forms asp.net-mvc-4 view http-post