我尝试使用Javascript和Ajax从bootstrap模式弹出窗口向我的控制器发送一个参数,但是当我点击按钮时,它不能在控制器上工作。我该如何发送这个参数?
这是我的模态HTML代码
<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">×</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代码
@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>
}
和我的控制器,这些代码甚至无法触发
[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浏览器,我无法在代码上设置断点。
答案 0 :(得分:0)
您的Javascript函数存在许多问题,这里有正确的问题:
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 () {
alert("done");
}
});
}
答案 1 :(得分:0)
将所有输入放在表单标记中:
<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">×</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代码
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 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);
}