我有一个在我的JsonResult中创建的ViewModel,在操作结束时,我传递了我的视图模型return Json(VMpartnum,JsonRequestBehavior.AllowGet);接下来,当我使用[httppost]单击按钮时,参数中的viewmodel为null。我的json中的所有内容都不存在。我如何将我的ViewModel从这个json传递到我的发布动作?我的猜测是我成功设置ajax的某种方式,但我不确定。
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
//var selected = $("#DDLpartbook option").prop("selected", true);
//url: '/Parts/DoPartBookFunc?bookinfo=' + $(this).attr("value"),
//alert("V " + selectV + "\nT " + selectT);
//var option = $(this).find("option:selected").text();
$.ajax({
url: '/Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
$("#txtNextPart").val(data.PartNextNumber);
},
error: function (e) {
alert("e " + e.message);
}
});
});
public JsonResult DoPartBookFunc(int bookval)
{
var VMpartnum = new PartNumberViewModel();
if(bookval != -1)
{
var book = context.PartBooks.Where(x => x.ID.Equals(bookval)).FirstOrDefault();
if(book != null)
{
var recordPartNumber = (from pnum in context.PartNumbers
where pnum.PartGroup == book.Series
orderby pnum.Last4Number descending
select pnum).Take(10).ToList();
var recordPartBook = (from pbook in context.PartBooks
where pbook.Series == book.Series
select pbook).FirstOrDefault();
var biggestPNumRecord = recordPartNumber.FirstOrDefault();
var nextEstimated = 0;
/* Load up my VIEWMODEL */
//VMpartnum.PartBook = biggestPNumRecord.PartGroup;
VMpartnum.PartBook = recordPartBook.Series;
VMpartnum.PartGroup = biggestPNumRecord.PartGroup;
VMpartnum.Last4Number = biggestPNumRecord.Last4Number;
if (biggestPNumRecord.Last4Number == "9999" || biggestPNumRecord.Last4Number == "-1")
{
TempData["Error"] = "Contact IT or Engineers 'Out of Part Numbers'";
//return RedirectToAction("TakeOutPart");
}
else
{
if(int.TryParse(biggestPNumRecord.Last4Number, out int last4plus1))
{
nextEstimated = last4plus1 + 1;
VMpartnum.PartNextNumber = VMpartnum.PartGroup + "." + nextEstimated;
}
else
{ // NOT INTEGER
try
{
//var nextNum = pnumRecord.Last4Number;
//nextEstimated = Convert.ToInt32(pnumRecord.Last4Number);
//VMpartnum.PartNextNumber = VMpartnum.PartGroup + "." + pnumRecord.Last4Number;
VMpartnum.PartNextNumber = VMpartnum.PartNumber;
}
catch (Exception ex) { }
}
//var groupNum = biggestPNumRecord.PartGroup;
//ViewBag.NextPartNumber = groupNum + "." + nextEstimated;
}
}
else
{
TempData["Error"] = "Couldn't find Partgroup|Series number";
}
}
//ViewBag.NextPartNumber = VMpartnum.PartNextNumber;
return Json(VMpartnum, JsonRequestBehavior.AllowGet);
}