我正在使用ajax调用一个动作控制器,但是当动作控制器收到它时,我用data
属性传递的参数总是为空的...
这可能会发生什么?
JQuery函数:
function PostOrder()
{
var id = $(".aslink").data("customerid");
var url = $("#btnAddOrderPost").data("url_add_order");
$.ajax({
type:"post",
url: url,
data: JSON.stringify( { orderVM: $("#frmCreatePV").serialize()}),
datatype: "json",
contentType: "application/json",
success: function () {
alert("it was inserted");
}
})
}
动作控制器:
[HttpPost]
// [ValidateAntiForgeryToken]
public ActionResult CreatePV(OrderVM orderVM)
{
if (ModelState.IsValid)
{
List<string> top = new List<string>();
decimal tempPrice = 0M;
for (int i = 0; i < orderVM.Toppings.Count; i++)
{
if (orderVM.Toppings[i].IsSelected == true)
{
top.Add(orderVM.Toppings[i].SpecificTopping);
tempPrice += orderVM.Toppings[i].Price;
}
}
Order order = new Order
{
Toppings = top,
TotalPrice = tempPrice
};
db.Orders.Add(order);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(orderVM);
}
这是OrderVM ViewModel我使用的是这种类型的参数:
public class OrderVM
{
public virtual List<ToppingVM> Toppings { get; set; }
public decimal TotalPrice { get; set; }
}
这是包含在部分视图中的表单:
@using (Html.BeginForm(null,null,FormMethod.Post, htmlAttributes: new { @id="frmCreatePV"}))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@{
for (int i = 0; i < Model.Toppings.Count; i++)
{
<div class="col-xs-4">
@Html.HiddenFor(model => model.Toppings[i].SpecificTopping)
@Html.CheckBoxFor(model => model.Toppings[i].IsSelected, htmlAttributes: new { data_price = Model.Toppings[i].Price, @id = "chbkPrice" })
@Html.HiddenFor(model => model.Toppings[i].Price)
@Html.LabelFor(model => model.Toppings[i].IsSelected , Model.Toppings[i].SpecificTopping)
<p>Price: @Model.Toppings[i].Price</p>
</div>
}
}
</div>
<div class="form-group">
<div class="col-md-10">
<input type="button" value="Add order" id="btnAddOrderPost" class="btn btn-primary"
data-url_add_order="@Url.Action("CreatePV", "Orders")" />
</div>
</div>
</div>
}
更新
最后,这就是行动控制员的表现:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult CreatePV(OrderVM orderVM)
{
int id = Convert.ToInt32(TempData["License"]);
if (ModelState.IsValid)
{
List<string> top = new List<string>();
decimal tempPrice = 0M;
for (int i = 0; i < orderVM.Toppings.Count; i++)
{
if (orderVM.Toppings[i].IsSelected == true)
{
top.Add(orderVM.Toppings[i].SpecificTopping);
tempPrice += orderVM.Toppings[i].Price;
}
}
Order order = new Order
{
Customer = db.Customers.Where(c => c.LicenseNumber == id).First(),
LicenseNumber = id,
Toppings = top,
TotalPrice = tempPrice
};
db.Orders.Add(order);
db.SaveChanges();
return Json(new { success= true, JsonRequestBehavior.AllowGet});
}
return Json(new { success = false, JsonRequestBehavior.AllowGet});
}
和JQuery函数一样,请注意我使用serializeArray()将一个元素添加到未绑定到html表单的数据中:
function PostOrder()
{
var orderVM = {};
id = $(".aslink").data("customerid");
alert($("#btnGetOrderAdd").data("customerid"));
var url = $("#btnAddOrderPost").data("url_add_order");
var datavar = $("#frmCreatePV").serializeArray();
datavar.push({name: "LicenseNumber" ,value : id})
$.ajax({
type:"post",
url: url,
data: datavar,
datatype: "json",
success: function () {
alert("it was inserted");
}
})
}
答案 0 :(得分:2)
删除contentType
对您有用。
contentType
是您要发送的数据类型,因此application / json; charset = utf-8是常见的,就像application / x-www-form-urlencoded一样; charset = UTF-8,这是默认值。
使用contentType: 'application/json'
时,您将无法依赖$ _POST进行填充。 $ _POST仅填充表单编码的内容类型。
在这种情况下,您可以访问PHP原始数据。
$input = file_get_contents('php://input');
$object = json_encode($input);
希望这可以帮助你:)