Skip to content Skip to sidebar Skip to footer

Passing Data From Modal Popup To Controller

I'm trying send a params from bootstrap modal popup to my controlller, using Javascript and Ajax, but when I click button doesn't work on controller. How can I send this params? He

Solution 1:

Put all input inside form tag:

<formenctype="multipart/form-data"><divclass="modal fade"role="dialog"id="mymodal"><divclass="modal-dialog modal-lg"><divclass="modal-content"><divclass="modal-header"><buttonclass="close"type="button"data-dismiss="modal">&times;</button><labelfor="infoh"id="info"name="info"></label><inputtype="hidden"id="infoh"name="infoh"value="" /></div><divclass="modal-body"><divclass="row"><divclass="col-md-3">
                        @Html.Label("Product : ")
                    </div><divclass="col-md-3"><inputtype="number"class="input-sm"id="product"name="product"/></div></div><br /><divclass="row"><divclass="col-md-3">
                    @Html.Label("Price : ")
                </div><divclass="col-md-3"><inputtype="number"class="input-sm"id="price"name="price"/></div></div></div><divclass="modal-footer"><buttonclass="btn btn-success"id="change"onclick="func(this)"name="change">@Html.Label("change") </button></div></div></div></form>

JavaScript code

functionfunc(x) {
    var pricee = document.getElementById("price").value;
    var productt = document.getElementById("product").value;
    var info = document.getElementById("infoh").value;
    $.ajax({
        url: '@Url.Content("~/myController/Action/")',
        type: 'POST',
        dataType: 'application/json',
        data: { 'info': info, 'price': pricee, 'product': productt },
         success: function (response) {
            alert(responseText.text);
        }
    });

}

Take JSON post methode:

   [HttpPost]
    public JsonResult Action(string info, double price, double product)
    {
        db Entities updateaction = new dbEntities();
        int id = (Convert.ToInt32(Session["id"]));

        string myinfo = info;
        Product pp = updateaction.Product.Where(m => m.database_id.Equals(id) && m.name.Equals(myinfo)).SingleOrDefault();
        pp.price = price;
        pp.product = product;
        int i = updateaction.SaveChanges();
        Session["warning"] = i;
        return Json(new { success = true, responseText = " Sucessfully." }, JsonRequestBehavior.AllowGet);
    }

Solution 2:

There are many issues with your Javascript function, and here the correct one:

functionfunc(x) {
        var pricee = document.getElementById("price").value;
        var productt = document.getElementById("product").value;
        var info = document.getElementById("infoh").value;
        $.ajax({
            url: '@Url.Content("~/myController/Action/")',
            type: 'POST',
            dataType: 'application/json',
            data: { 'info': info, 'price': pricee, 'product': productt },
            success: function () {
                alert("done");
            }
        });

    }

Post a Comment for "Passing Data From Modal Popup To Controller"