Using Bootstrap Modal window as PartialView

后端 未结 5 997
南方客
南方客 2020-11-27 10:05

I was looking to using the Twitter Bootstrap Modal windows as a partial view. However, I do not really think that it was designed to be used in this fashion; it seems like

5条回答
  •  自闭症患者
    2020-11-27 11:03

    I use AJAX to do this. You have your partial with your typical twitter modal template html:

    Then you have your controller method, I use JSON and have a custom class that rendors the view to a string. I do this so I can perform multiple ajax updates on the screen with one ajax call. Reference here: Example but you can use an PartialViewResult/ActionResult on return if you are just doing the one call. I will show it using JSON..

    And the JSON Method in Controller:

    public JsonResult LocationNumberModal(string partNumber = "")
    {
      //Business Layer/DAL to get information
      return Json(new {
          LocationModal = ViewUtility.RenderRazorViewToString(this.ControllerContext, "LocationNumberModal.cshtml", new SomeModelObject())
        },
        JsonRequestBehavior.AllowGet
      );
    }
    

    And then, in the view using your modal: You can package the AJAX in your partial and call @{Html.RenderPartial... Or you can have a placeholder with a div:

    then your ajax:

    function LocationNumberModal() {
      var partNumber = "1234";
    
      var src = '@Url.Action("LocationNumberModal", "Home", new { area = "Part" })'
        + '?partNumber='' + partNumber; 
    
      $.ajax({
        type: "GET",
        url: src,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
          $("#LocationNumberModalContainer").html(data.LocationModal);
          $('#LocationNumberModal').modal('show');
        }
      });
    };
    

    Then the button to your modal:

    
    
    | |

提交回复
热议问题