0
我有一個post方法,參數School
每次調用時都是空的。我一定要發送正確的數據,我使用Postman,但是當我用斷點檢查它時,我得到一個空對象。爲什麼我的發佈方法沒有收到它發送的數據?
我與郵差派:
{
"Id": "",
"Name": "Test"
}
控制器:
[HttpPost]
public IActionResult Post([FromBody] School school)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest();
}
var schoolData = Mapper.Map<School, Data.School>(school);
var schoolModel = _schoolRepository.AddSchool(schoolData);
return CreatedAtRoute("GetSchool", new { id = schoolModel }, schoolModel);
}
catch (Exception e)
{
return LogException(e);
}
}
型號:
public class School
{
public Guid Id { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public IEnumerable<Teacher> Teachers { get; set; }
public IEnumerable<Class> Classes { get; set; }
}
問題是''「'不是一個有效的'Guid'。如果你不想發送'Id',將它從模型綁定中排除 –
@CamiloTerevinto是的,你說的沒錯,我只是想,因爲「Id」沒有必要的屬性,所以應該讓它通過。你可以添加這個作爲答案和惡意標記 –