【发布时间】:2015-10-10 20:23:56
【问题描述】:
我有一个 MVC5 应用程序,它有一个方法填充并返回一个局部视图。由于该方法接受 ID 作为参数,因此如果未提供 ID,则 Id 会返回错误。
[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 作为其输出的方法,报告错误的正确方法是什么?
【问题讨论】:
标签: asp.net-mvc model-view-controller error-handling viewmodel partial-views