我是MVC5的新手。我正在尝试使用ActionResult来检索主数据和详细信息数据,以及在ActionLink中单击检索数据的JsonResult for Details。
public JsonResult getOrderDetails(int? id)
{
List<OrderDetail> OrderDetail = new List<OrderDetail>();
OrderDetail = db.OrderDetails.Where(a => a.OrderID==id).OrderBy(a => a.OrderDetialsID).ToList();
return new JsonResult { Data = OrderDetail, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
OrderMaster OrderMaster = db.OrderMasters.Find(id);
return View(OrderMaster);
}
答案 0 :(得分:1)
不,这不起作用。它将抛出一个异常,解释链接必须指向一个资源(a.k.a一个Controller,一个Action)。而且,这不是你通常认为在MVC中这样做的方式。这不是单独加载主数据和详细信息的WebForms。
你应该做这样的事情:
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var orderMaster = db.OrderMasters
.Where(om => om.OrderMasterId == id)
.Include(om => om.OrderDetails) // include the details here
.Single();
return View(orderMaster);
}