在我的MVC程序中,一旦用户提交了编辑表单,该对象的这些值将保存在模型服务器端,但之前的值将显示在视图中。
我知道它与MVC的验证过程有关,它首先在服务器端值之前检查ModelState。我在论坛上阅读的解决方案是清除ModelState。唯一的问题是,ModelState.Clear对我不起作用。
请帮助。
模型
public class Help
{
[HiddenInput(DisplayValue=true)]
public int HelpID { get; set; }
[Required(ErrorMessage = "Please enter a proper URL")]
public string URL { get; set; }
[Required(ErrorMessage = "Please enter a content description:")]
[DataType(DataType.MultilineText)]
public string HelpContent { get; set; }
/*? 2 properites are nullable*/
public DateTime? createDateTime { get; set; }
public DateTime? modifiedDateTime { get; set; }
}
控制器
/*Create the admin controller*/
public class AdminController : Controller
{
//declare interface object
private IHelpRepository repository;
/*Pass a db interface to controller*/
public AdminController(IHelpRepository repo)
{
repository = repo;
}
/*default admin screen. displays help table obs*/
public ViewResult Index()
{
return View();
}
/*Returns add view form*/
public ActionResult AddForm()
{
return PartialView();
}
/*Will handle the post for the add screen after user has
submitted add information*/
[HttpPost]
[ValidateInput(false)] //this allows admin to place html in field
public ActionResult AddForm(Help help)
{
if (ModelState.IsValid) //if all fields are validated
{
//set the edit date
help.createDateTime = DateTime.Now;
repository.SaveHelp(help);
return (null); //return "null" to div so control is given back to main view
}
else //there is something wrong. send back to view
{
return PartialView(help);
}
}
/*Returns edit view form, searches for object to edit with id
if no id provided, 0 by default*/
public ActionResult EditForm(int helpID = 0)
{
Help help = repository.Help.FirstOrDefault(q => q.HelpID == helpID);
help.HelpContent = System.Web.HttpUtility.HtmlDecode(help.HelpContent);
return PartialView(help);
}
/*Will handle the post for the edit screen after user has
submitted edit information*/
[HttpPost]
[ValidateInput(false)] //this allows admin to place html in field
public ActionResult EditForm(Help help)
{
if (ModelState.IsValid) //if all fields are validated
{
//set the edit date
help.modifiedDateTime = DateTime.Now;
repository.SaveHelp(help);
ModelState.Clear();
return (null); //return "null" to div so control is given back to main view
}
else //there is something wrong. send back to view
{
return PartialView(help);
}
}
/*Delete action method, searches with id*/
[HttpPost]
public ActionResult Delete(int helpId)
{
Help helpDel = repository.Help.FirstOrDefault(p => p.HelpID == helpId);
if (helpDel != null) //if the object is found, delete
{
repository.DeleteHelp(helpDel);
}
//in all cases return to index
return RedirectToAction("Index");
}
/*Used by the telerik table to rebind grid*/
[GridAction]
public ActionResult AjaxBinding()
{
return View(new GridModel(repository.Help));
}
}//end admin controller class`
部分视图(获取加载到div中) `
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Editx" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit Entry</legend>
@Html.HiddenFor(model => model.HelpID)
<div class="editor-label">
@Html.LabelFor(model => model.URL)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.URL)
@Html.ValidationMessageFor(model => model.URL)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.HelpContent, "Help Content")
</div>
<div class="editor-field">
@{
Html.Telerik().EditorFor(content => content.HelpContent)
.Name("HelpContent")
.FileBrowser(settings => settings
.Browse("Browse", "ImageBrowser")
.Thumbnail("Thumbnail", "ImageBrowser")
.Upload("Upload", "ImageBrowser")
.DeleteFile("DeleteFile", "ImageBrowser")
.DeleteDirectory("DeleteDirectory", "ImageBrowser")
.CreateDirectory("CreateDirectory", "ImageBrowser")
)
.Render();
}
@Html.ValidationMessageFor(model => model.HelpContent)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.createDateTime, "Create Date")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.createDateTime)
@Html.ValidationMessageFor(model => model.createDateTime)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.modifiedDateTime, "Modified Date")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.modifiedDateTime)
@Html.ValidationMessageFor(model => model.modifiedDateTime)
</div>
<p>
<input id="btnEdit" type="submit" value="Save" />
<button id="btnCancel">Cancel</button>
</p>
</fieldset>
}
答案 0 :(得分:0)
在浏览了大约一百个链接之后,我找到了解决问题的方法。 ModelState.Clear实际上清除了控制器中的对象值,但无论出于何种原因仍然在视图中显示旧值。也许是因为我将编辑表单加载/卸载到div标签中?不要问。我不知道。对我有用的解决方案是:
$.ajax({
url: "somecontroller/someAction,
cache: false, // this is key to make sure JQUERY does not cache your request
success: function( data ) {
alert( data ); } });
我必须将“缓存”设置为“false”。
感谢@ minus4的解决方案,布鲁赫。