如何在 MVC 3.0 中的视图之间传递数据列表?

发布于 2024-11-05 13:18:39 字数 813 浏览 1 评论 0原文

在遵循音乐商店教程之后,我正在构建我的第一个 MVC 应用程序,并且在尝试在强类型视图之间传递数据时遇到了一些问题。

我有两种数据类型,它们都有自己的强类型视图

客户 客户站点

在客户站点的索引视图(返回一些客户站点)中,我需要在每个站点地址旁边显示客户名称,该视图对客户站点进行强类型化,但客户名称存储在客户表中,因此它不允许我直接从模型访问这个?

我知道一种选择是使用 viewbag,我已成功完成此操作,使用以下代码在创建视图的下拉列表中显示客户名称:

客户站点控制器/////////

public ActionResult Create()
    {
        ViewBag.Id = new SelectList(db.Customers, "Id", "CustomerName");
        return View();
    } 

索引视图// ////////

@Html.DropDownListFor(model => model.CustomerId,   (IEnumerable<SelectListItem>)ViewBag.Id)

这工作正常,但现在我需要将客户名称插入每个表行而不是下拉列表。任何人都可以提供有关如何检索要在客户站点索引视图的表格中呈现的客户名称列表的任何指示吗?

viewbag 是在视图之间传递数据的最佳方式还是还有其他替代方法?有没有办法对具有多种数据类型的视图进行强类型化?

任何人都可以提供的任何建议都会很棒。

亲切的问候

利亚姆

I am building my first MVC app after following the Music Store tutorial and have ran into some problems when trying to pass data between strongly typed views.

I have two datatypes with their own strongly typed views

Customer
Customer Site

In the index view of customer site (which returns a lits of customer sites) i need to display the customer name next to each site address, the view is strongly typed to customer site but the customer name is stored in the customer table so it wont allow me to access this directlty from the model?

I know that one option is to use the viewbag, i have successfully done this to display the Customer Name in a drop down list on my create view using the following code

Customer Site Controller/////////

public ActionResult Create()
    {
        ViewBag.Id = new SelectList(db.Customers, "Id", "CustomerName");
        return View();
    } 

Index view//////////

@Html.DropDownListFor(model => model.CustomerId,   (IEnumerable<SelectListItem>)ViewBag.Id)

This works fine but now i need to the customer name to be inserted each table row rather than a drop down list. Can anyone kindly offer any pointers on how i can retrieve a list of customer names to be rendered in a table in the index view of the customer site?

Also is the viewbag the best way to pass data between views or are there other alternative methods? Is there a way to strongly type a view with multiple data types?

Any advice anyone can offer would be great.

Kind Regards

Liam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-11-12 13:18:39

针对您问题的这一部分:

viewbag也是最好的方式
在视图之间传递数据或存在
其他替代方法?有没有一个
强类型视图的方法
多种数据类型?

答案是肯定的。像这样创建一个新类:

public class SomeViewModel {
    public Customer SomeCustomer { get; set; }

    public CustomerSite SomeCustomerSite { get; set; }
}

将其传递到您的视图中,然后视图将可以通过其模型访问这两种数据类型。

In response to this part of your question:

Also is the viewbag the best way to
pass data between views or are there
other alternative methods? Is there a
way to strongly type a view with
multiple data types?

The answer is Yes. Create a new class like so:

public class SomeViewModel {
    public Customer SomeCustomer { get; set; }

    public CustomerSite SomeCustomerSite { get; set; }
}

Pass that into your view which will then have access to both data types through its Model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文