在寫mvc項目時,一個列表查詢頁面含有多個操做按鈕及彈框操做。本來寫在了一個view中,致使代碼繁多複雜,難以維護,還有表單賦值清空、驗證等麻煩。mvc
所以改用kendo window +partialView的方式,代碼清潔,方便維護。也能夠實現複用。post
一、當前view中添加kendo window 彈框ui
@(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()) )
二、grid列表操做一行時,彈出操做框url
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();
}
三、controller action代碼spa
[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;
}
}
四、PartialViewcode
@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>
五、PartialView驗證的坑orm
參照blog
https://stackoverflow.com/questions/9490322/mvc-3-razor-partial-view-validation-is-not-workingip
在partialView加載渲染後須要從新解析form的客戶端驗證。而且須要在提交按鈕時驗證下formci
<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>