我试图清除表单控制器并使用ModelState.Clear();但是不行。我希望在将表单保存到数据库后清除表单。是否有可能刷新视图并清除表单jquery。
<td>
@Ajax.ActionLink("Show Post Order", "_ListPost", new { idOrder = @item.IdOrder }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoading",
UpdateTargetId = "divPosition",
InsertionMode = InsertionMode.Replace
}, new { @class = "btn btn-primary" })
</td>
<td>
@Ajax.ActionLink("Add Answer", "_AddPost", new { idZlecenia = @item.IdZlecenia }, new AjaxOptions()
{
HttpMethod = "GET",
LoadingElementId = "divLoadingForm",
UpdateTargetId = "divAddPozycje",
InsertionMode = InsertionMode.Replace
}, new { @class = "btn btn-primary" })
</td>
</tr>
...
<tr id="divAddPozycje"></tr>
}
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult _AddPost(int idOrder, AddPositionViewModel viewModel)
{
var findOrder = db.Order.Find(idOrder);
if (ModelState.IsValid)
{
OrderPosition position = new OrderPosition { Description = viewModel.Description };
db.OrderPosition.Add(position);
findOrder.OrderPositionList.Add(position);
db.SaveChanges();
ViewBag.Information = position;
}
Thread.Sleep(2000);
ModelState.Clear();
return PartialView();
}
并查看_AddPost - Paritial View。这个partialView添加位置 命令。使用不添加项目的Html Ajax添加项目 页面刷新
@model AplikacjaHelpDesk.ViewModels.AddPositionViewModel
@{
ViewBag.Title = "Add Post";
Layout = null;
}
<link href="~/Content/font-awesome.min.css" rel="stylesheet" />
<script src="~/Scripts/tinymce/tinymce.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/jquery-2.2.3.min.js"></script>
<div class="container-fluid">
@using (Ajax.BeginForm(new AjaxOptions()
{
UpdateTargetId = "divformResult",
HttpMethod = "Post"
}))
{
@Html.AntiForgeryToken()
@Html.Hidden("IdOrder")
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 ">
<input type="submit" value="Save" class="btn btn-danger" />
</div>
</div>
</div>
}
</div>
...