1
方法返回錯誤請求我有有一個方法填充和返回的局部視圖的MVC5應用。由於該方法接受一個ID作爲參數,標識喜歡如果沒有提供它返回一個錯誤。在返回PartialViewResult
[HttpGet] public PartialViewResult GetMyData(int? id)
{
if (id == null || id == 0)
{
// I'd like to return an invalid code here, but this must be of type "PartialViewResult"
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); // Does not compile
}
var response = MyService.GetMyData(id.Value);
var viewModel = Mapper.Map<MyData, MyDataViewModel>(response.Value);
return PartialView("~/Views/Data/_MyData.cshtml", viewModel);
}
對於返回PartialViewResult作爲其輸出的方法報告錯誤的正確方法是什麼?
說實話,我希望的返回值,所以我的前端會顯示這是與應用中的一切相一致的錯誤消息。但是,這也會在一個緊要關頭。 – Rethic
如果需要顯示特定的你可以隨時使用'ViewBag'的做到這一點的看法什麼。以上是我如何實現這一點。 –