[英]MVC 3 - Passing model to controller from different controller
目前,这就是我的HomeController
:
[HttpPost]
public ActionResult Index(HomeFormViewModel model)
{
...
...
TempData["Suppliers"] = service.Suppliers(model.CategoryId, model.LocationId);
return View("Suppliers");
}
这就是我的SupplierController
:
public ViewResult Index()
{
SupplierFormViewModel model = new SupplierFormViewModel();
model.Suppliers = TempData["Suppliers"] as IEnumerable<Supplier>;
return View(model);
}
这是我的Supplier
Index.cshtml
:
@model MyProject.Web.FormViewModels.SupplierFormViewModel
@foreach (var item in Model.Suppliers) {
...
...
}
除了使用TempData
,还有其他方法可以将对象传递到不同的控制器及其视图?
您为什么不只是将这两个ID作为参数传递,然后从另一个控制器调用服务类? 就像是:
具有您的SupplierController
方法,如下所示:
public ViewResult Index(int categoryId, int locationId)
{
SupplierFormViewModel model = new SupplierFormViewModel();
model.Suppliers = service.Suppliers(categoryId, locationId);
return View(model);
}
然后,假设您要通过某种链接从“ Supplier
视图中调用视图? 你可以做:
@foreach (var item in Model.Suppliers)
{
@Html.ActionLink(item.SupplierName, "Index", "Supplier", new { categoryId = item.CategoryId, locationId = item.LocationId})
//The above assumes item has a SupplierName of course, replace with the
//text you want to display in the link
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.