我在视图中显示两个部分视图。部分视图显示良好。但是如果模型值为空,如何在我的部分视图中显示错误消息。下面是我的代码:
控制器:
public ActionResult MyAccount(string email)
{
OrderModel objorder = new OrderModel();
var OrderDetails = objorder.GetOrderInfo(email);
if(OrderDetails == null)
{
// ViewBag.Message = "You have not placed any orders.";
ModelState.AddModelError("NoOrders", "You have not placed any orders.");
return View();
}
CustomerModel objcustomer = new CustomerModel();
var CustomerDetails = objcustomer.GetCustomerInfo(email);
return View(Tuple.Create(OrderDetails, CustomerDetails));
}
看法
@model Tuple<IList<MvcApplication1.Models.OrderModel>, IList<MvcApplication1.Models.CustomerModel>>
@Html.Partial("MyOrders")<br/>
@Html.Partial("AccountInfo")
部分视图 1 @model 元组,IList>
@using (Html.BeginForm())
{
if (Model.Item1 != null) // Null reference unhandled by user code
{
<div>
@foreach(var item in Model.Item1)
{
@Html.DisplayFor(model => item.OrderID)
}
</div>
}
else
{
<p>@Html.ValidationMessage("NoOrders", new { @class = "msg" })</p>
}
}
我已通过验证消息以在 Partial View1 中处理空值。但是我收到未设置对象引用和未处理空引用的消息。
我的代码中有什么错误。?任何建议都会有所帮助