将集合中的模型传递给actionlink [英] Passing model in collection to actionlink

查看:92
本文介绍了将集合中的模型传递给actionlink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个非常基本的问题.我试图实现的是将对象集合显示为链接.当我单击一个链接时,我希望被带到该特定对象的详细信息.

I feel like this is a pretty basic question. What I am trying to achieve is display a collection of objects as links. When I click on a link, I want to be taken to that specific object's details.

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

I can display a collection of item links on the index view, but when I click an item link, I can display the SingleProductView, but cannot display that specific item's variables there.

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

Is it possible to pass the specific item to the view through the html.actionlink? Or, is it possible to pass that specific item to another action that will display a view?

模型:

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

推荐答案

您说的return View();并不是您要传递的模型.它是空的.因此,检索模型(通常是从数据库中检索模型,但在这种情况下,仅使用实例字段),然后将其传递给视图.

When you say return View();, you aren't passing it a model. It's empty. So retrieve a model (usually from a database, but in your case just using an instance field) and pass that to the 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作为模型类型.

Your view should accept a ProductModel as the model type.

@* 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,该ID将变成操作方法的参数(假设您已使用默认路由).在索引视图中更改为此的链接:

You don't pass the product from the index view to the other view, you pass the ID in the URL, which will turn into a parameter for the action method (assuming you've used default routing). Change your link to this in your index view:

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

您的ProductModel表示您具有ProductId属性,而没有ListPrice属性.我认为您需要添加public double ListPrice {get; set;},然后在创建广告资源时分配ID,例如:

Your ProductModel says you have a ProductId property, and no ListPrice property. I think you need to add a public double ListPrice {get; set;} and then when you create your inventory, assign ID's, for example:

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.

The URL for accessing a product with an ID of 1 should be (assuming default routing) /Home/SingleProductView/1.

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

By the way, you should rename ProductModel to Product. That makes it a little cleaner. And rename ProductName to Name. Look at the difference: ProductModel.ProductName vs Product.Name. Both are just as clear, but one is way more concise.

这篇关于将集合中的模型传递给actionlink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆