更新时间:2023-02-25 17:29:36
下面是我得到了这个工作。
关键的一点是: 我需要使用与视图关联的视图模型,以便在运行时能够解决的对象的要求。
[我知道,有一种方法来绑定对象而不是默认的视图模型对象等,但最终只是填充必要属性,我的需要,因为我无法得到它的工作]
[HttpPost]
公众的ActionResult GetDataForInvoiceNumber(MyViewModel myViewModel)
{
VAR invoiceNumberQueryResult = _viewModelBuilder.HydrateMyViewModelGivenInvoiceDetail(myViewModel.InvoiceNumber,myViewModel.SelectedCompany code);
返回JSON(invoiceNumberQueryResult,JsonRequestBehavior.DenyGet);
}
用于调用该操作方法的jQuery脚本:
VAR的RequestData = {
InvoiceNumber:$ .trim(THIS.VALUE)
SelectedCompany code:$ .trim($('#SelectedCompany code')VAL())
};
$阿贾克斯({
网址:'/ EN / myController的/ GetDataForInvoiceNumber',
键入:POST,
数据:JSON.stringify(的RequestData)
数据类型:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
错误:函数(XHR){
警报('错误:'+ xhr.statusText);
},
成功:函数(结果){
CheckIfInvoiceFound(结果);
},
异步:真正的,
过程数据:假的
});
I am trying to achieve a JQuery AJAX call to a controller action method that contains a complex object as a parameter. I have read plenty blogs and tried several techniques learned from these. The key post on which I have constructed my best attempt code (below) is the *** post here .
I want to trigger an asynchronous post, invoked when the user tabs off a field [not a Form save post – as demonstrated in other examples I have found].
My intention is to:
The results will be returned as a JSON result; and data will be loaded into fields in the view depending on results returned.
The problems are:
Hopefully someone can help. Thanks. Code below:
Client js file
var disputeKeyDataObj = {
"InvoiceNumber": "" + $.trim(this.value) + "",
"CustomerNumber": "" + $.trim($('#CustomerNumber').val()) + ""
};
var disputeKeyDataJSON = JSON.stringify(disputeHeadlineData);
$.ajax({
url: "/cnr/GetDataForInvoiceNumber",
type: "POST",
data: disputeKeyDataJSON,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: EnrichedDisputeKeyData(result)
});
Action Filter and class for the type associated with the Action method parameter
[DataContract]
public class DisputeKeyData
{
[DataMember(Name = "InvoiceNumber")]
public string InvoiceNumber { get; set; }
[DataMember(Name = "CustomerNumber")]
public string CustomerNumber { get; set; }
}
Action method on the controller
//[HttpPost]
[ObjectFilter(Param = "disputeKeyData", RootType = typeof(DisputeKeyData))]
public ActionResult GetDataForInvoiceNumber(DisputeKeyData disputeKeyData)
{
//Blah!
//....
return Json(disputeKeyData, JsonRequestBehavior.AllowGet);
}
Below is how I got this working.
The Key point was: I needed to use the ViewModel associated with the view in order for the runtime to be able to resolve the object in the request.
[I know that that there is a way to bind an object other than the default ViewModel object but ended up simply populating the necessary properties for my needs as I could not get it to work]
[HttpPost]
public ActionResult GetDataForInvoiceNumber(MyViewModel myViewModel)
{
var invoiceNumberQueryResult = _viewModelBuilder.HydrateMyViewModelGivenInvoiceDetail(myViewModel.InvoiceNumber, myViewModel.SelectedCompanyCode);
return Json(invoiceNumberQueryResult, JsonRequestBehavior.DenyGet);
}
The JQuery script used to call this action method:
var requestData = {
InvoiceNumber: $.trim(this.value),
SelectedCompanyCode: $.trim($('#SelectedCompanyCode').val())
};
$.ajax({
url: '/en/myController/GetDataForInvoiceNumber',
type: 'POST',
data: JSON.stringify(requestData),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
success: function (result) {
CheckIfInvoiceFound(result);
},
async: true,
processData: false
});