C# MVC 3-从不同控制器向控制器传递模型

C# MVC 3-从不同控制器向控制器传递模型,c#,asp.net,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Razor,目前,这是我的HomeController中的内容: 这是我在SupplierController中的内容: 是否有不同的方法将对象传递给不同的控制器及其视图而不是使用TempData?为什么不将这两个ID作为参数传入,然后从另一个控制器调用服务类?比如: 让您的SupplierController方法如下: public ViewResult Index(int categoryId, int locationId) { SupplierFormViewModel model = ne

目前,这是我的HomeController中的内容:

这是我在SupplierController中的内容:


是否有不同的方法将对象传递给不同的控制器及其视图而不是使用TempData?

为什么不将这两个ID作为参数传入,然后从另一个控制器调用服务类?比如:

让您的SupplierController方法如下:

public ViewResult Index(int categoryId, int locationId)
{
    SupplierFormViewModel model = new SupplierFormViewModel();
    model.Suppliers = service.Suppliers(categoryId, locationId);

    return View(model);
}
那么,我假设您是通过某种链接从供应商视图中调用您的视图?你可以做:

@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
}

为什么不将这两个ID作为参数传入,然后从另一个控制器调用服务类呢?比如:

让您的SupplierController方法如下:

public ViewResult Index(int categoryId, int locationId)
{
    SupplierFormViewModel model = new SupplierFormViewModel();
    model.Suppliers = service.Suppliers(categoryId, locationId);

    return View(model);
}
那么,我假设您是通过某种链接从供应商视图中调用您的视图?你可以做:

@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
}

为什么SupplierController不能为自己调用服务。供应商?@Jeroen我已经更新了问题并添加了该服务。供应商从HomeFormViewModel获取属性。为什么SupplierController不能为自己调用服务。供应商?@Jeroen我已经更新了问题并添加了该服务。供应商从HomeFormViewModel获取属性HomeFormViewModel,明白了!我知道这一点,我只是太累了,无法编写任何合适的代码。哈哈,我听到了,听起来像是星期五综合症:明白了!我知道这一点,我只是太累了,无法编写任何合适的代码。哈哈,我听到了,听起来像是星期五综合症:
@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
}