C# 使用jQuery关闭包含部分视图(MVC4)的引导模式
我有一个显示在引导模式窗口中的局部视图。视图是用于创建对象实例的Ajax表单 当我单击save按钮时,对象被正确地添加到数据库中,但模式保持打开状态。我希望在单击“保存”按钮时关闭模式 模式中的按钮:C# 使用jQuery关闭包含部分视图(MVC4)的引导模式,c#,jquery,asp.net-mvc-4,twitter-bootstrap,C#,Jquery,Asp.net Mvc 4,Twitter Bootstrap,我有一个显示在引导模式窗口中的局部视图。视图是用于创建对象实例的Ajax表单 当我单击save按钮时,对象被正确地添加到数据库中,但模式保持打开状态。我希望在单击“保存”按钮时关闭模式 模式中的按钮: <div class="modal-footer"> <button class="btn btn-inverse" type="submit" id="btn-create-property-save">Save</button> </div&g
<div class="modal-footer">
<button class="btn btn-inverse" type="submit" id="btn-create-property-save">Save</button>
</div>
当我单击按钮时,模态保持打开状态
有一件事我不太确定,这可能是导致问题的原因,那就是我在CreateObject
控制器操作中返回的内容。目前我只有returnpartialview()代码>。鉴于上述情况,我应该返回什么
编辑:添加代码以显示如何加载模态
<div class="modal hide fade in" id="create-residential-property-modal" data-url="@Url.Action("LandlordProperties", "Landlords")">
<div id="create-property-container"></div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#show-create-property-modal').click(function () {
var url = $('#create-residential-property-modal').data('url');
$.get(url, function (data) {
$('#create-property-container').html(data);
$('#create-residential-property-modal').modal('show');
});
});
});
</script>
$(文档).ready(函数(){
$(“#显示创建属性模式”)。单击(函数(){
var url=$('#创建住宅物业模式')。数据('url');
$.get(url、函数(数据){
$('#创建属性容器').html(数据);
$(“#创建住宅物业模式”).model('show');
});
});
});
以下是我过去是如何做到这一点的
第1步,使用Ajax提交表单:
@using (Ajax.BeginForm("Create", new AjaxOptions()
{
OnFailure = "Modal.onAjaxFailure",
OnSuccess = "Modal.onAjaxSuccess"
}))
{
<div class="form-body">
@Html.TextAreaFor(model => model.Text, new { rows = "3" })
</div>
<div class="form-footer">
@Html.ValidationMessageFor(model => model.Text)
@Html.ValidationSummary(true)
@CustomHelpers.SubmitButton("Add Comemnt")
</div>
}
[HttpPost]
public ActionResult Create(CommentCreateModel model)
{
if (ModelState.IsValid)
{
//Do your database insert etc here...
return Json(new { error = false, message = "Comment added" });
}
else
{
return Json(new { error = true, message = "The values entered are not valid" });
}
}
export function onAjaxFailure(xhr, status, error) {
//Show notifications for AJAX errors
$.notify({
type: "error",
text: error
});
}
export function onAjaxSuccess(data, status, xhr) {
if (data.error) {
$.notify({
type: "error",
text: data.message
});
}
else {
$('.modal').modal('hide'); //Hides modal window
//Show message/anything else here
}
}
步骤3,处理响应(我的示例是TypeScript):
@using (Ajax.BeginForm("Create", new AjaxOptions()
{
OnFailure = "Modal.onAjaxFailure",
OnSuccess = "Modal.onAjaxSuccess"
}))
{
<div class="form-body">
@Html.TextAreaFor(model => model.Text, new { rows = "3" })
</div>
<div class="form-footer">
@Html.ValidationMessageFor(model => model.Text)
@Html.ValidationSummary(true)
@CustomHelpers.SubmitButton("Add Comemnt")
</div>
}
[HttpPost]
public ActionResult Create(CommentCreateModel model)
{
if (ModelState.IsValid)
{
//Do your database insert etc here...
return Json(new { error = false, message = "Comment added" });
}
else
{
return Json(new { error = true, message = "The values entered are not valid" });
}
}
export function onAjaxFailure(xhr, status, error) {
//Show notifications for AJAX errors
$.notify({
type: "error",
text: error
});
}
export function onAjaxSuccess(data, status, xhr) {
if (data.error) {
$.notify({
type: "error",
text: data.message
});
}
else {
$('.modal').modal('hide'); //Hides modal window
//Show message/anything else here
}
}
以下是我过去是如何做到这一点的
第1步,使用Ajax提交表单:
@using (Ajax.BeginForm("Create", new AjaxOptions()
{
OnFailure = "Modal.onAjaxFailure",
OnSuccess = "Modal.onAjaxSuccess"
}))
{
<div class="form-body">
@Html.TextAreaFor(model => model.Text, new { rows = "3" })
</div>
<div class="form-footer">
@Html.ValidationMessageFor(model => model.Text)
@Html.ValidationSummary(true)
@CustomHelpers.SubmitButton("Add Comemnt")
</div>
}
[HttpPost]
public ActionResult Create(CommentCreateModel model)
{
if (ModelState.IsValid)
{
//Do your database insert etc here...
return Json(new { error = false, message = "Comment added" });
}
else
{
return Json(new { error = true, message = "The values entered are not valid" });
}
}
export function onAjaxFailure(xhr, status, error) {
//Show notifications for AJAX errors
$.notify({
type: "error",
text: error
});
}
export function onAjaxSuccess(data, status, xhr) {
if (data.error) {
$.notify({
type: "error",
text: data.message
});
}
else {
$('.modal').modal('hide'); //Hides modal window
//Show message/anything else here
}
}
步骤3,处理响应(我的示例是TypeScript):
@using (Ajax.BeginForm("Create", new AjaxOptions()
{
OnFailure = "Modal.onAjaxFailure",
OnSuccess = "Modal.onAjaxSuccess"
}))
{
<div class="form-body">
@Html.TextAreaFor(model => model.Text, new { rows = "3" })
</div>
<div class="form-footer">
@Html.ValidationMessageFor(model => model.Text)
@Html.ValidationSummary(true)
@CustomHelpers.SubmitButton("Add Comemnt")
</div>
}
[HttpPost]
public ActionResult Create(CommentCreateModel model)
{
if (ModelState.IsValid)
{
//Do your database insert etc here...
return Json(new { error = false, message = "Comment added" });
}
else
{
return Json(new { error = true, message = "The values entered are not valid" });
}
}
export function onAjaxFailure(xhr, status, error) {
//Show notifications for AJAX errors
$.notify({
type: "error",
text: error
});
}
export function onAjaxSuccess(data, status, xhr) {
if (data.error) {
$.notify({
type: "error",
text: data.message
});
}
else {
$('.modal').modal('hide'); //Hides modal window
//Show message/anything else here
}
}
莫代尔的名字是什么?您确定它的id为“创建住宅物业模式”!检查浏览器提供的源代码。modal的名称是什么?您确定它的id为“创建住宅物业模式”!检查浏览器提供的源代码。现在还没有时间尝试此操作,我会告诉您何时我会这样做。几乎在那里,模式窗口关闭,但屏幕仍然具有第一次启动模式时出现的变暗效果-有没有关于如何补救此问题的想法?如何加载模式窗口?你能用js代码更新原始问题吗?你知道如何在模式关闭时消除屏幕黑暗吗?这个调用是“$.get(url,函数(数据)”吗在控制器中返回PartialView?如果你不返回PartialView,你会得到一个黑色覆盖层,在模式关闭时不会消失。现在没有时间尝试,我会告诉你。模式窗口几乎会关闭,但屏幕仍然有第一次启动模式时出现的变暗效果-有什么想法吗n如何解决此问题?如何加载模式窗口?能否使用js代码更新原始问题。是否知道如何在模式关闭时消除屏幕黑暗?此调用是否为“$.get(url,函数(数据)”在控制器中返回PartialView?如果不返回PartialView,则会得到一个黑色覆盖层,在模式关闭时不会消失。