Pass (Send) Model as parameter from Bootstrap Modal PopUp to Controller in ASP.Net MVC

bigbear
 
on Nov 13, 2019 11:29 PM
5157 Views

Hello all,

I am just simply trying to pass my model in my method as a parameter so I have all the data in my model.

I can put the model in a Viewbag to pass it in that works fine. But why can't I pass it in as a parameter instead?

The button I am using is on a modal which is inside of the main view.

        [HttpPost]
        public ActionResult UnZipDownload(ServiceModel model)
        {
            if(TempData["ZipName"] != null)
            {
                bool isValid = false;
                string zipName = TempData["ZipName"] as string;
                string downloadPath = new KnownFolder(KnownFolderType.Downloads).Path;
                string filePath = new KnownFolder(KnownFolderType.Downloads).Path;
                downloadPath = Path.GetFullPath(downloadPath);
                //Make sure last char on path doesn't allow malicious code outside of it.
                if (!downloadPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                    downloadPath += Path.DirectorySeparatorChar;

                string zipFolder = zipName.Substring(0, zipName.Length - 4);
                Directory.CreateDirectory(Path.Combine(downloadPath, zipFolder));
                TempData["FolderName"] = zipFolder;
                using (ZipArchive archive = ZipFile.OpenRead(filePath + "\\" + zipName))   // downloadPath + zipName
                {
                    filePath += "\\" + zipFolder;       //  /?/ downloadPath + zipFolder ??  ??

                    foreach(ZipArchiveEntry entry in archive.Entries)
                    {
                        if(entry.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
                        {                            
                            string destinationPath = Path.GetFullPath(Path.Combine(filePath, entry.FullName));
                            entry.ExtractToFile(destinationPath);
                        }
                    }
                    // Unzipped all files into new folder completed
                    isValid = true;
                }
                var mod = ViewBag.ServModel;
                var servModel = TempData["ServModel"];

                if (isValid)
                {
                    ViewBag.AlertSuccess = "The .zip file has been successfully unzipped";
                    OpenPDFs();
                }
            }
            return RedirectToAction("VirtualService", model);
        }

 

<div id="zipModal" class="modal" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            @using (Html.BeginForm("UnZipDownload", "Service", FormMethod.Post, new { model = Model }))
            {
                <div class="modal-header text-center">

                </div>
                <div class="modal-body text-center">

                </div>
                <div class="modal-footer">
                    @*Html.ActionLink("OK", "UnZipDownload", "Service", new { @model = Model, @class = "btn btn-success pull-right" })*@
                    <input type="submit" name="submit" class="btn btn-success pull-right" value="OK"/>
                </div>
            }

        </div>
    </div>
</div>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Nov 13, 2019 11:29 PM

I will get back to you soon.

dharmendr
 
on Nov 14, 2019 06:43 AM

Hi bigbear,

Check this example. Now please take its reference and correct your code.

Model

public class CustomerModel
{
    [Required(ErrorMessage = "Name is required.")]
    public string Name { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View(new CustomerModel());
    }

    [ValidateAntiForgeryToken]
    [HttpPost]
    [HandleError]
    public ActionResult UpdateServiceTables(CustomerModel model)
    {
        return PartialView("_SearchCustomer", model);
    }

    public JsonResult GetServiceQuote(int? id)
    {
        if (id == null) { return null; }
        var vm = new CustomerModel();
        vm.Name = "Test";
        return Json(vm, JsonRequestBehavior.AllowGet);
    }
}

View

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('.openModal').on('click', function () {
            var quoteid = $(this).attr("data-quoteid");
            $.ajax({
                type: "GET",
                url: "/Home/GetServiceQuote/",
                contentType: "application/json; charset=utf-8",
                data: { "id": quoteid },
                datatype: "Json",
                success: function (data) {
                    var modalA = $('#modalInnerSQ');
                    var body = $('.modal-body');
                    var quoteid = modalA.find(body).find('#Name');
                    $(quoteid).val(data.Name);
                    OpenServiceQuoteModal();
                }
            });
        });
    });

    function OpenServiceQuoteModal() {
        $('#modalInnerSQ').modal('show');
    }
</script>
<input type="button" class="openModal btn btn-primary" data-quoteid="1" value="Open" />
<%using (Html.BeginForm("UpdateServiceTables", "Home", FormMethod.Post, new { model = Model }))
    {%>
<div class="modal fade" id="modalInnerSQ" tabindex="-1" role="dialog" aria-labelledby="lblSQmodal"
    aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">
                    <b>Service Quote Details</b></h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="modalbody">
                <%:Html.Partial("_SearchCustomer")%>
            </div>
            <div class="modal-footer">
                <input type="submit" name="submit" class="btn btn-success pull-right" value="Ok" />
            </div>
        </div>
    </div>
</div>
<% } %>

PartialView

<%:Html.AntiForgeryToken() %>
<%:Html.ValidationSummary(true, "", new { @class = "text-danger" })%>
<table>
    <tr>
        <td>
            Name
        </td>
        <td>
            <%:Html.TextBoxFor(model => model.Name)%>
            <%:Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })%>
        </td>
    </tr>
</table>

Screenshot