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'的做到这一点的看法什么。以上是我如何实现这一点。 –