将数据从模式弹出窗口传递到控制器 [英] Passing data from modal popup to controller

查看:43
本文介绍了将数据从模式弹出窗口传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript和Ajax将引导程序模式弹出窗口中的参数发送到我的控制器,但是当我单击按钮在控制器上不起作用时.我该如何发送这些参数?

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?

这是我的模式HTML代码

Here is my modal HTML codes

<div class="modal fade" role="dialog" id="mymodal">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button class="close" type="button" data-dismiss="modal">&times;</button>

            <label for="infoh" id="info" name="info"></label>
          <input type="hidden" id="infoh" name="infoh" value="" />
        </div> 
        <div class="modal-body">

                <div class="row">
                    <div class="col-md-3">
                        @Html.Label("Product : ")
                    </div>
                    <div class="col-md-3">
                        <input type="number" class="input-sm"  id="product" name="product"/>
                    </div>
            </div><br />

            <div class="row">
                <div class="col-md-3">
                    @Html.Label("Price : ")
                </div>
                <div class="col-md-3">
                    <input type="number" class="input-sm" id="price" name="price"/>
                </div>
            </div>

        </div>
        <div class="modal-footer">
            <button class="btn btn-success" id="change"  onclick="func(this)"  name="change">@Html.Label("change") </button>
        </div>
    </div>
</div>

模态效果很好该Jscript代码

Modal works fine This Jscript codes

@section Scripts{
<script type="text/javascript">




  func(x) {
      var pricee = document.getElementById("price").value;
      var productt = document.getElementById("product").value;
      var info = document.getElementById("infoh").value;
      $.ajax({
          url: 'myController/Action',
          type: 'POST',
          data: { 'info': info, 'price': pricee, 'product': prdocutt },
          success: function () {
              alert("done");
          }
      });

            }



</script>

}

和我的控制器,甚至不触发

and my Controller,this codes doesn't work not even trigger

  [HttpPost]
    public ActionResult 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 View();
    }

我正在Opera Opera浏览器上工作,我无法在代码上加上断点.

I'm working on Opera Browser and I can't put breakpoint on my codes.

推荐答案

将所有输入内容置于表单标签内:

Put all input inside form tag:

<form enctype="multipart/form-data">
<div class="modal fade" role="dialog" id="mymodal">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button class="close" type="button" data-dismiss="modal">&times;</button>

            <label for="infoh" id="info" name="info"></label>
          <input type="hidden" id="infoh" name="infoh" value="" />
        </div> 
        <div class="modal-body">

                <div class="row">
                    <div class="col-md-3">
                        @Html.Label("Product : ")
                    </div>
                    <div class="col-md-3">
                        <input type="number" class="input-sm"  id="product" name="product"/>
                    </div>
            </div><br />

            <div class="row">
                <div class="col-md-3">
                    @Html.Label("Price : ")
                </div>
                <div class="col-md-3">
                    <input type="number" class="input-sm" id="price" name="price"/>
                </div>
            </div>

        </div>
        <div class="modal-footer">
            <button class="btn btn-success" id="change"  onclick="func(this)"  name="change">@Html.Label("change") </button>
        </div>
    </div>
</div>
</form>

JavaScript代码

JavaScript code

function func(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);
        }
    });

}

采用JSON发布方法:

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);
    }

这篇关于将数据从模式弹出窗口传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆