【问题标题】:Passing model in collection to actionlink将集合中的模型传递给 actionlink
【发布时间】:2015-02-11 20:56:24
【问题描述】:

我觉得这是一个非常基本的问题。我想要实现的是将对象集合显示为链接。当我点击一个链接时,我想被带到那个特定对象的详细信息。

我可以在索引视图上显示项目链接的集合,但是当我单击项目链接时,我可以显示 SingleProductView,但不能在那里显示该特定项目的变量。

是否可以通过 html.actionlink 将特定项目传递给视图?或者,是否可以将该特定项目传递给另一个将显示视图的操作?

型号:

public class ProductModel
{
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
}

家庭控制器:

public class HomeController : Controller
{

    List<ProductModel> inventory = new List<ProductModel>() {
        new ProductModel { ProductName = "White T-Shirt", ProductDescription = "White T-Shirt", ListPrice = 10 },
        new ProductModel { ProductName = "Black T-Shirt", ProductDescription = "Black T-Shirt", ListPrice = 10 },
    };

    public ActionResult Index()
    {
        return View(inventory);
    }

    [HttpGet]
    public ActionResult SingleProductView()
    {
        return View();
    }
}

索引视图:

   @if(Model != null)
       {
            <ul>
            @foreach (ProductModel item in Model)
            {
                <li>
                    @Html.ActionLink(item.ProductName, "SingleProductView")
                </li>
            }
            </ul>
       }

【问题讨论】:

    标签: asp.net model actionresult html.actionlink


    【解决方案1】:

    当你说return View(); 时,你并没有传递给它一个模型。它是空的。因此,检索一个模型(通常从数据库中,但在您的情况下只使用实例字段)并将其传递给视图。

    [HttpGet]
    public ActionResult SingleProductView(int id)
    {
        //From the inventory, retrieve the product that has an ID that matches the one from the URL (assuming default routing)
        //We're using Linq extension methods to find the specific product from the list.
        ProductModel product = inventory.Where(p => p.ProductId == id).Single();
    
        //Send that product to the view.
        return View(product);
    }
    

    您的视图应该接受 ProductModel 作为模型类型。

    @* Declare the type of model for this view as a ProductModel *@
    @model ProductModel
    
    @* Display the product's name in a header. Model will be an instance of ProductModel since we declared it above. *@
    <h2>@Model.ProductName</h2>
    
    @* Display the product's description in a paragraph *@
    <p>@Model.ProductDescription</p>
    

    您不会将产品从索引视图传递到另一个视图,而是在 URL 中传递 ID,这将变成操作方法的参数(假设您使用了默认路由)。在您的索引视图中将您的链接更改为:

    @Html.ActionLink(item.ProductName, "SingleProductView", new {Id = item.ProductId})
    

    您的ProductModel 表示您有ProductId 属性,但没有ListPrice 属性。我认为您需要添加 public double ListPrice {get; set;},然后在创建库存时分配 ID,例如:

    List<ProductModel> inventory = new List<ProductModel>() {
        new ProductModel { ProductId = 1, ProductName = "White T-Shirt", ProductDescription = "White T-Shirt", ListPrice = 10 },
        new ProductModel { ProductId = 2, ProductName = "Black T-Shirt", ProductDescription = "Black T-Shirt", ListPrice = 10 },
    };
    

    访问 ID 为 1 的产品的 URL 应该是(假设默认路由)/Home/SingleProductView/1

    顺便说一句,您应该将ProductModel 重命名为Product。这使它更干净一些。并将ProductName 重命名为Name。看看区别:ProductModel.ProductNameProduct.Name。两者都一样清晰,但一个更简洁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多