简体   繁体   English

如果是条件,则从控制器返回JsonResult中的模态确认框

[英]Return a modal confirmation box in a JsonResult from controller if a condition

Hi everyone i have the following situation: I have a view in which I have a save button that is serializing a form and sending it by Ajax to a JsonResult in the Controller. 大家好我有以下情况:我有一个视图,其中我有一个保存按钮,序列化一个表单,并通过Ajax发送到Controller中的JsonResult。 Inside this JsonResult I'm adding/editing tables in the database. 在这个JsonResult里面,我在数据库中添加/编辑表。 My question is if is possible to return a confirmation box to the view if a certain condition exists. 我的问题是,如果存在某种条件,是否可以向视图返回一个确认框。 Bellow is my code. 贝娄是我的代码。 Thanks in the advance :) 谢谢你提前:)

This is the javascript in my view that is sending the form-data to the controller by ajax. 这是我视图中的javascript,它通过ajax将表单数据发送到控制器。

<script>
    function submitForm() {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("UpdateEvent", "EventCalendar")',
            data: $('#UpdateEventForm').serialize(),
            success: function (eventoId) {
                $('#modal-edit-event').modal('hide');
                $('#calendar').fullCalendar('refetchEvents');

            }
        });
    }

This is my Controller code that is receiving the form-data: 这是我接收表单数据的Controller代码:

 public JsonResult UpdateEvent(.........)
 {
   List<Guid> usersChanged = new List<Guid>();
   .
   .
   .
   if(usersChanged.Count() > 0)
   {
    //Here is where i want to call a confirmation box
   }
   .
   .
   .
   return Json(eventId, JsonRequestBehavior.AllowGet);
}

} }

Yes it is possible. 对的,这是可能的。 All you have to do is, return the HTML markup needed to show in the modal dialog. 您所要做的就是返回在模式对话框中显示所需的HTML标记。

Here, i am using the RenderRazorViewToString method (from the Ben Lesh's answer on Render a view as a string ) 在这里,我使用的是RenderRazorViewToString方法(来自Ben Lesh的答案, 将视图渲染为字符串

public string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                                    viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                        ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}

Now, you just need to call this method as needed to get the string version of your viewresult 现在,您只需要根据需要调用此方法来获取viewresult的字符串版本

public ActionResult UpdateEvent()
{

    if (SomeConditionHere)
    {
       var modalMarkup = RenderRazorViewToString("MyModalView", null);
       return Json(new { status = "failed", data = modalMarkup },
                                                            JsonRequestBehavior.AllowGet);
    }
    return Json(new { status = "success", eventId = 123 }, JsonRequestBehavior.AllowGet);
}

Assuming you have a view called MyModalView.cshtml which has the markup needed for the bootstrap modal dialog as below 假设您有一个名为MyModalView.cshtml的视图,该视图具有引导模式对话框所需的标记,如下所示

<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                   <span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    </div>
    <div class="modal-body">
        <h2>Modal content</h2>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
    </div>
</div>

Make sure your ajax call's success event callback method is checking the json data coming back and based on the status property value, use the data /eventId as needed. 确保您的ajax调用的成功事件回调方法正在检查返回的json数据,并根据status属性值,根据需要使用data / eventId。

success: function (res) {
    $("#myModal").remove();
    if (res.status === "failed") {
        $('<div id="myModal" class="modal fade"><div class="modal-dialog" role="document">'
                +res.data 
                +'</div></div>')
            .modal();
    } else {
        alert("eventId is : "+ res.eventId);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM