我正在使用Core 2.2项目 Db层的API项目 Web项目视图
我可以从API读取数据 但是我在发送数据进行更新时遇到问题
代码
1-从Web项目发送数据
[HttpPost]
public async Task<IActionResult> Edit(NationalityViewModel nationality )
{
LawyerAPI lawyerAPI = new LawyerAPI();
HttpClient httpClient = lawyerAPI.InitializeClient();
HttpResponseMessage res = await httpClient.PutAsJsonAsync("api/nofactory/updatenationality", nationality);
return View(nationality);
}
2-InitializeClient
private string _apiBaseURI = "http://localhost:57030";
public HttpClient InitializeClient()
{
var client = new HttpClient();
//Passing service base url
client.BaseAddress = new Uri(_apiBaseURI);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
3- API
[Route("updatenationality")]
public IActionResult Put(NationalityViewModel nationality)
{
var nat=new Nationality();
nat.Name=nationality.Name;
nat.IsActive=nationality.IsActive;
nat.Id=nationality.Id;
_NationalityRepository.Update(nat);
return Ok();
}
问题是Web项目中的国籍变量具有数据,并且当发送到API时它是空模型,断点命中了API方法,但是变量为空
我认为问题在于发送功能
谢谢
为澄清起见,我添加了图片
答案 0 :(得分:0)
您必须告诉框架从请求主体进行绑定。您可以通过使用NationalityViewModel
属性注释[FromBody]
参数来实现。
public IActionResult Put([FromBody] NationalityViewModel nationality)