0

I am trying to pass a model to a bootstrap modal editor. I call the modal from an Ajax method. I can get the model from the controller, and view its properties from the script's success event. However, I cannot send this model "mod" to my bootstrap modal body. mod is always null when I call my partial view inside in my modal body. Is it possible to open/show a Bootstrap modal with a value passed to the modal-body.

@model StockItemViewModel
@{
ViewData["Title"] = "ProductServiceIndex";
StockItemViewModel mod = Model;


<div class="modal fade" id="prodserviceModal" tabindex="-1" role="dialog" aria-labelledby="myModal-label">
<div class="modal-dialog vertical-align-center">
    <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="myModal-label">Product/Service Information</h4>
            <h4>@mod.StockItemName</h4>
        </div>
        <div class="modal-body" id="productServicePlaceholder">
            @await Html.PartialAsync("_ProductServiceModal", mod)
        </div>
    </div>
</div>

function ajax_get(data) {
    $.ajax({
        url: "/Purchasing/AddEditProdServ",
        type: "GET",           
        data: data,
        success: function (data) {
            //$('#productServicePlaceholder').html(data);
            mod = data;
            //alert(mod.StockItemId);
            $('#prodserviceModal').modal('show');
        }
    });  
user938455
  • 89
  • 11
  • https://stackoverflow.com/questions/39865828/update-data-using-ajax-modal-laravel/47363803#47363803 – Jasim Juwel Dec 14 '18 at 16:38
  • Try returning the partial view itself from your ajax function: https://stackoverflow.com/questions/10589787/asp-net-mvc-returning-a-partialview-to-ajax-along-with-another-object – Captain Red Dec 14 '18 at 17:15
  • While I would also return the partial view from the ajax as an html string then use `$("#productServicePlaceHolder").html(data)`, you can set individual inputs based on the data returned (none of which is in the question, so here's an example): `$("#productServicePlaceholder #ProductTitle").val(data.ProductTitle)` – freedomn-m Dec 14 '18 at 18:27
  • Ideally, I will like to just set the model in my partialasync call to the model returned from the controller. Is this possible? Each time I try it using the code provided earlier, my mod is returning null. – user938455 Dec 15 '18 at 17:43

0 Answers0