1

目前,这就是我的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是否有不同的方法将对象传递给不同的控制器及其视图,而不是使用?

4

1 回答 1

6

为什么不直接将这两个 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
}
于 2012-04-20T10:27:35.160 回答