mvc partialView+kendo window
在写mvc项目时,一个列表查询页面含有多个操作按钮及弹框操作。原本写在了一个view中,导致代码繁多复杂,难以维护,还有表单赋值清空、验证等麻烦。
因此改用kendo window +partialView的方式,代码清洁,方便维护。也可以实现复用。
1、当前view中添加kendo window 弹框
@(Html.Kendo().Window() .Name("partialViewWindow") .Title(Resources.OrderCheckFreeEquipmentScrap) .Modal(true) .Content(@<text> <div id="partialViewDiv"></div> </text>) .Draggable() .Resizable() .Width(600) .Visible(false) .Actions(actions => actions.Close()) )
2、grid列表操作一行时,弹出操作框
function agreeDrop(e) {
var dataItem = $("#EquipmentGrid").data("kendoGrid").dataItem($(e).closest("tr"));
var url = "@Url.Action("AgreeDropEquipment", "OrderCheckManage", new { id = "__id__" })";
$("#partialViewDiv").load(url.replace("__id__", dataItem.Id));
$("#partialViewWindow").data("kendoWindow").center().open();
}
3、controller action代码
[HttpGet]
public ActionResult AgreeDropEquipment(int id)
{
EquipmentDropModel model=new EquipmentDropModel
{
Id = id
};
return PartialView(model);
}
[HttpPost]
public ActionResult AgreeDropEquipment(EquipmentDropModel model)
{
try
{
var userId = UserId();
_commonService.ScrapEquipment(model.Id,model.DepreciationYear,model.SalvageValue,model.Comment,userId);
return RedirectToAction("Index", "OrderCheckManage");
}
catch (Exception exp)
{
_commonService.SaveLog(exp.ToString());
throw;
}
}
4、PartialView
@model DMS.WEB.Models.EquipmentDropModel <form action="@Url.Action("AgreeDropEquipment", "OrderCheckManage")" method="post" class="panel panel-default form-horizontal panel-body"> <div class="form-group"> @Html.HiddenFor(m => m.Id) @Html.RequiredIndicatorLabelFor(m => m.DepreciationYear, new { @class = "col-sm-3 control-label no-padding-right" }) <div class="col-sm-7"> @Html.TextBoxFor(m => m.DepreciationYear, "", new { @class = "form-control popupwindowinput" }) </div> </div> <div class="form-group"> @Html.RequiredIndicatorLabelFor(m => m.SalvageValue, new { @class = "col-sm-3 control-label no-padding-right" }) <div class="col-sm-7"> @Html.TextBoxFor(m => m.SalvageValue, "", new { @class = "form-control popupwindowinput" }) </div> </div> <div class="form-group"> @Html.RequiredIndicatorLabelFor(m => m.Comment, new { @class = "col-sm-3 control-label no-padding-right" }) <div class="col-sm-7"> @Html.TextAreaFor(m => m.Comment, new { @class = "form-control", rows = 3 }) </div> </div> <div class="form-group"> <div class="text-center"> <button class="btn btn-info" type="submit"> @Resources.CommonButtonSubmit </button> @*<button class="btn btn-info margin-left-5 closeWindowBtn" type="button"> @Resources.CommonButtonCancle </button>*@ </div> </div> </form>
5、PartialView验证的坑
参照
https://stackoverflow.com/questions/9490322/mvc-3-razor-partial-view-validation-is-not-working
在partialView加载渲染后需要重新解析form的客户端验证。并且需要在提交按钮时验证下form
<script> $(function () { jQuery.validator.unobtrusive.parse(); $('#importForm').removeData('validator'); $('#importForm').removeData('unobtrusiveValidation'); $.validator.unobtrusive.parse('#importForm'); $("#submitBtn").click(function() { if (!$("#importForm").valid()){ return false; }
$("#importForm").submit(); return true; }); }); </script>