我曾经使用另一个称为jquery modal的jquery库。但是,我决定切换回使用引导程序模式。
我不确定如何通过带有引导程序模式的url打开模式。这就是我以前用jquery模态完成的方式:
@Html.ActionLink("Add", "RoleDetails", @ViewContext.RouteData.Values["controller"].ToString(), new { Id = "", ApplicationId = Model.Id }, new { @class = "btn btn-primary", @rel = "modal:open" })
这是我的控制者:
public ActionResult RoleDetails(string Id, string ApplicationId)
{
ApplicationRoleDetailsViewModel role = new ApplicationRoleDetailsViewModel();
if (Id == null)
{
role = new ApplicationRoleDetailsViewModel
{
ApplicationId = null,
Id = Id,
RoleName = null
};
}
else
{
var result = rolesData.Get(Id);
role = new ApplicationRoleDetailsViewModel
{
ApplicationId = result.ApplicationId,
Id = Id,
RoleName = result.Name
};
}
return View("~/Areas/Applications/Views/Applications/Modals/RoleDetails.cshtml", role);
}
这是视图:
@model APPDATA.Data.ViewModels.ApplicationRoleDetailsViewModel
<script>
$(document).ready(function () {
});
$(document).on("submit","#modal",function (e) {
e.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url: "@Url.Action("SaveRole", @ViewContext.RouteData.Values["controller"].ToString())",
method: "POST",
data: form_data,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function (result) {
if (result.success) {
$(document).off("submit");
$(document).off("submit", "#save");
$('#modal').off('#save');
$.modal.close();
return;
}
$.each(result.errors, function (index, item) {
// Get message placeholder
var element = $('[data-valmsg-for="' + item.propertyName + '"]');
element.empty();
// Update message
element.append($('<span></span>').text(item.errorMessage));
// Update class names
element.removeClass('field-validation-valid').addClass('field-validation-error');
$('#' + item.propertyName).removeClass('valid').addClass('input-validation-error');
});
}
});
return false;
});
</script>
<div class="col-sm-12">
@if (Model.Id == null)
{
<h2>Add Role</h2>
}
else
{
<h2>Edit Role</h2>
}
<hr />
</div>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "modal" }))
{
@Html.AntiForgeryToken()
<div class="col-sm-12">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.ApplicationId)
@Html.LabelFor(m => m.RoleName)
@Html.TextBoxFor(m => m.RoleName, new { @class = "form-control col-sm-12" })
@Html.ValidationMessageFor(m => m.RoleName)
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<div class="clearfix">
<div class="pull-right">
<input type="submit" id="save" value="Save" class="btn btn-primary" />
<a href="#" id="close" class="btn btn-outline-secondary" rel="modal:close">Cancel</a>
</div>
</div>
</div>
</div>
</div>
</div>
}
我将如何看待如下所示的模态?
<div class="modal" id="modal">
<div class="modal-dialog">
<div class="modal-content" id="modal-content">
</div>
</div>
</div>
答案 0 :(得分:0)
如果要将“ RoleDetails.cshtml”加载到模态内容中,请使用ajax调用而不是ActionLink。
从控制器返回部分视图,而不是传递视图。
因此insde ajax调用将使您将整个视图作为数据添加到模型内容中。
当您单击链接时,执行ajax调用,并在获得成功响应后打开如下所示的模型。
<a href="#" id='btnAdd'>Add</a>
<script>
$('#btnAdd').on('click',function(){
$.ajax({
url : '@Url.action("action","controller")',
data : data,
success : function(data){
$('#model-content').html(data);
$.modal.open();
}
});
});
</script>