在过去的几个小时里,我一直在尝试通过Ajax将Json对象发送到我的Action,数据到达动作,但其值为null。
如果需要任何其他信息,我很乐意更新问题。
谢谢, 伊兰
jQuery:
$(function () {
$(".button-search").click(function () {
var manufacturer= $("#Manufacturer").val();
var model = $("#Model").val();
var rentPrice = $("#RentPrice").val();
var gear = $("#Gear").val();
var year = $("#Year").val();
$.ajax({
type: "POST",
url: "/Car/SearchResults",
data: {
Manufacturer: manufacturer,
Model: model,
RentPrice: rentPrice,
Gear: gear,
Year: year,
},
success: function (results) {
alert("success");
},
error: function (error) {
alert("error");
}
})
});
})
动作:
[HttpPost]
public ActionResult SearchResults(CarSearchModel model)
{
var searchResults = CarHelper.CarSearch(model.Manufacturer, model.Model, model.RentPrice, model.Year, model.Gear);
JsonResult result = new JsonResult();
result.Data = searchResults;
return result;
}
类别:
public class CarSearchModel
{
[StringLength(4, ErrorMessage="Maximum 4 digits.")]
public int Year { get; set; }
public string Model { get; set; }
public string Manufacturer { get; set; }
public string Gear { get; set; }
[DataType(DataType.Currency)]
[Display(Name="Rent Price")]
public int RentPrice { get; set; }
[Display(Name="Delay Fee")]
public int DelayFee { get; set; }
[DataType(DataType.Date)]
[Required]
[Display(Name="Rent Date")]
public DateTime RentDate { get; set; }
[DataType(DataType.Date)]
[Required]
[Display(Name = "Return Date")]
public DateTime ReturnDate { get; set; }
}
答案 0 :(得分:0)
你需要添加:
dataType: "json"
$.ajax({
type: "POST",
url: "/Car/SearchResults",
dataType: "json",
data: {
Manufacturer: manufacturer,
Model: model,
RentPrice: rentPrice,
Gear: gear,
Year: year,
},
success: function (results) {
alert("success");
},
error: function (error) {
alert("error");
}
})
答案 1 :(得分:0)
尝试这个应该对你有用,你也需要打开你的开发工具(F12)并添加你得到的错误细节。
$(function () {
$(".button-search").click(function () {
var manufacturer= $("#Manufacturer").val();
var model = $("#Model").val();
var rentPrice = $("#RentPrice").val();
var gear = $("#Gear").val();
var year = $("#Year").val();
var input = {
Manufacturer: manufacturer,
Model: model,
RentPrice: rentPrice,
Gear: gear,
Year: year
};
$.ajax({
type: "POST",
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: "/Car/SearchResults",
data: JSON.stringify(input),
success: function (results) {
alert("success");
},
error: function (error) {
alert("error");
}
})
});
})
这可能会对您有所帮助:How to call web api method using jquery ajax in ASP.NET MVC 4
答案 2 :(得分:0)
我尝试使用示例应用程序开发类似的代码,这里是类似Ajax结果的工作代码。 注意:ActionResult应该是JsonResult
[HttpPost]
public JsonResult Index(Practice model)
{
// Whatever your logic
return Json(new { msg = model.PracticeName }, JsonRequestBehavior.AllowGet);
}
像这样的Ajax请求,
$(document).ready(function () {
$('#btn').click(function (ev) {
ev.preventDefault();
$.ajax({
url: '@Url.Action("Index","Practices")',
type: 'Post',
contenttype:'application/json; charset=utf-8',
data: {
PracticeId: $('#PracticeName').val() ,
PracticeName:$('#PracticePercentage').val()
},
success: function (data) {
alert(data.msg);
}
});
});
});
我的示例视图就是这样,
@model MvcApplication3.Models.Practice
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Practice</legend>
<div class="editor-label">
@Html.LabelFor(model => model.PracticeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PracticeName)
@Html.ValidationMessageFor(model => model.PracticeName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PracticePercentage)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PracticePercentage)
@Html.ValidationMessageFor(model => model.PracticePercentage)
</div>
<p>
<input type="button" value="Create" id="btn" name="btn" />
</p>
</fieldset>
}
我的样本模型就像这里
public class Practice
{
public int PracticeId { get; set; }
public string PracticeName { get; set; }
public string PracticePercentage { get; set; }
}
希望这可以帮助您解决问题。
<强>更新强>
在您的方案中删除&#34;年&#34;的验证属性从ViewModel中再试一次它工作正常。还要从视图模型中重命名Model属性以进行冲突。
就是这样,
class CarViewModel {
public int Year { get; set; }
public string Manufacturer { get; set; }
public string Gear { get; set; }
public int RentPrice { get; set; }
[Display(Name = "Delay Fee")]
public int DelayFee { get; set; }
[DataType(DataType.Date)]
[Required]
[Display(Name = "Rent Date")]
public DateTime RentDate { get; set; }
[DataType(DataType.Date)]
[Required]
[Display(Name = "Return Date")]
public DateTime ReturnDate { get; set;
}
答案 3 :(得分:0)
我建议检查ModelState以查看是否存在任何类型转换错误。
我相信您会收到如下所示的错误。
The parameter conversion from type 'System.String' to type 'xxx' failed because
no type converter can convert between these types.
这可能是由于您的控制器参数名称与&#39;模型&#39;相同。财产名称。
答案 4 :(得分:0)
我对你的代码做了一些改动,它对我有用。
<强>模型强>
public class CarSearchModel
{
public string Manufacturer {get;set;}
public string Model {get;set;}
public string RentPrice {get;set;}
public string Year {get;set;}
public string Gear {get;set;}
}
行动方法
[HttpPost]
public ActionResult SearchResults(CarSearchModel model1)
{
return Json(model1);
}
Jquery Post
$(function(){
$('.button-search').click(function(){
var data1= {
Manufacturer: 'Audi',
Model: 'Q8',
RentPrice: '100',
Gear: '4',
Year: '2001'
};
$.ajax({
url: '@Url.RouteUrl(new{ action="SearchResults", controller="Home"})',
data: JSON.stringify(data1),
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(resp) {
alert(JSON.stringify(resp));
}
});
return false;
});
});
您只需要对json参数进行字符串化。构建并运行,它将起作用。