当我尝试发布到Web API时,接收到的值为null [英] The received value is null when I try to post to my web api
本文介绍了当我尝试发布到Web API时,接收到的值为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Web API创建mvc应用程序,我正在使用ajax调用(jquery)在数据库中插入客户详细信息,我正在将调用路由到控制器中的
i am creating mvc application using web api i am inserting the customer detail in database using ajax call(jquery) i am routing my call to
"LeadCustomer/SaveClient"
.在jquery端,我在chrome浏览器中调试了我的代码,我能够填充Data1,它是JSON STRING,但在控制器端,data1为null,不胜感激.json字符串不为null我调试了它
我尝试过的事情:
in my controller. on jquery side i debugged my code in chrome browser i am able to populate Data1 which is JSON STRING but on controller side DATA1 is null any help is appreciated.json string is not null i debugged it
What I have tried:
function insertInformation(name, telephone, email, Gender, birthday, cityid, stateid, Token, stageid, riskProfile, createdBy) {
//var Data1 = null;
debugger;
var Data1 = { CUSTOMER_NAME: name, MOBILE: telephone, EMAIL_ID: email, GENDER: Gender, DOB: birthday, CITY_ID: cityid, STATE_ID: stateid, TOKEN_ID: Token, STAGE_ID: stageid, CREATED_BY: createdBy, UPDATED_BY: Token };
//Data1 = { CUSTOMER_NAME: name },
alert(JSON.stringify(Data1));
$.ajax({
type: "POST",
url: apiUrl + "LeadCustomer/SaveClient",
data: JSON.stringify(Data1),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
async: true,
headers: { "cache-control": "no-cache" },
success: function (obj) {
var result = obj;
alert(result);
},
error: function (obj) {
alert("error");
}
});
}
public class LeadCustomerEntity
{
//Unique Token ID
public string GENDER { get; set; }
public string CUSTOMER_NAME { get; set; }
public int MOBILE { get; set; }
public string EMAIL_ID { get; set; }
public string DOB { get; set; }
public int STATE_ID { get; set; }
public int CITY_ID { get; set; }
public string CREATED_BY { get; set; }
public string UPDATED_BY { get; set; }
public string TOKEN_ID { get; set; }
public string STAGE_ID { get; set; }
}
[HttpPost]
[Route("LeadCustomer/SaveClient", Name = "SaveClient")]
public IHttpActionResult SaveClient([FromBody] LeadCustomerEntity Data1)
//public IHttpActionResult SaveClient(string CUSTOMER_NAME)
{
try
{
// SaveConfirmation items = OrderRepository.SaveOrder(Data);
LeadCustomerMosl item = LeadCustomerRepository.LeadCustomerRegistration(Data1);
// string item = "heelo world";
return Ok(item);
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
return Ok("Failed");
}
}
推荐答案
.ajax({ 类型:" , url:apiUrl + " , 数据: JSON .stringify(Data1), contentType:" , dataType:" , 缓存: false , 异步: true , 标头:{" :" 无缓存"}, 成功:功能(obj){ var result = obj; 警报(结果); }, 错误:功能(obj){ alert(" ); } }); }
.ajax({ type: "POST", url: apiUrl + "LeadCustomer/SaveClient", data: JSON.stringify(Data1), contentType: "application/json; charset=utf-8", dataType: "json", cache: false, async: true, headers: { "cache-control": "no-cache" }, success: function (obj) { var result = obj; alert(result); }, error: function (obj) { alert("error"); } }); }
public class LeadCustomerEntity
{
//Unique Token ID
public string GENDER { get; set; }
public string CUSTOMER_NAME { get; set; }
public int MOBILE { get; set; }
public string EMAIL_ID { get; set; }
public string DOB { get; set; }
public int STATE_ID { get; set; }
public int CITY_ID { get; set; }
public string CREATED_BY { get; set; }
public string UPDATED_BY { get; set; }
public string TOKEN_ID { get; set; }
public string STAGE_ID { get; set; }
}
[HttpPost]
[Route("LeadCustomer/SaveClient", Name = "SaveClient")]
public IHttpActionResult SaveClient([FromBody] LeadCustomerEntity Data1)
//public IHttpActionResult SaveClient(string CUSTOMER_NAME)
{
try
{
// SaveConfirmation items = OrderRepository.SaveOrder(Data);
LeadCustomerMosl item = LeadCustomerRepository.LeadCustomerRegistration(Data1);
// string item = "heelo world";
return Ok(item);
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
return Ok("Failed");
}
}
在发出ajax请求时,您已经写了"url:apiUrl +"LeadCustomer/SaveClient","
您错过了提供其URL的地方.
When you are making ajax request you have written "url: apiUrl + "LeadCustomer/SaveClient","
You have missed to provide its url.
这篇关于当我尝试发布到Web API时,接收到的值为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文