【问题标题】:Returning a list to strongly typed view using viewbag使用 viewbag 将列表返回到强类型视图
【发布时间】:2011-08-20 03:02:00
【问题描述】:

我正在尝试使用 viewbag 将客户列表返回到强类型视图,并且由于我是 MVC 和 Razor 的新手,我遇到了一些困难,有人可以就以下问题提供任何建议吗?

我的控制器中有以下代码,

  public ViewResult Index()
    {
        var q = from a in db.Customers
        select a;
        ViewBag.customer = q;
        return View(db.CustomerSites.ToList());
    }

我认为这段代码

@foreach (var customer in Model)
{
<tr>
 <td>
        @ViewBag.customer.CustomerName
   <td>
   <td>
        @customer.UnitNo
    </td>
    <td>
        @Truncate(customer.StreetName, 25)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=customer.Id }) |
        @Html.ActionLink("Details", "Details", new { id=customer.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=customer.Id })
    </td>
   </tr>
}

所以我正在检索客户和客户站点的列表,这些站点被键入到视图中,

@model IEnumerable<trsDatabase.Models.CustomerSite>

我正在尝试使用

从客户列表中提取 CustomerName
@ViewBag.customer.CustomerName

但这会产生一个错误,说“客户”类型不包含客户名称的定义,但它确实如下所示,所以我不确定为什么会发生错误。

namespace trsDatabase.Models
{
    public class Customer
    {

        [ScaffoldColumn(false)]
        public int Id { get; set; }
        [StringLength(50), Required(ErrorMessage = "Customer name required, please enter a customer name")]
        public string CustomerName { get; set; }
        [DisplayName("Contact Name")]
        [StringLength(50), Required(ErrorMessage = "Primary contact name required, please enter a contact name")]
        public string PrimaryContactName { get; set; }
        [DisplayName("Secondary Contact")]
        public string SecondaryContactName { get; set; }
        [DisplayName("Email")]
        [StringLength(50), Required(ErrorMessage = "Primary email address is required please enter an email address")]
        public string PrimaryEmailAddress { get; set; }
        [DisplayName("Secondary Email")]
        public string SecondaryEmailAddress { get; set; }
        [ScaffoldColumn(false)]
        public DateTime RegisteredDate { get; set; }
        [DisplayName("Contact No")]
        public string PrimaryContactNo { get; set; }
        [DisplayName("Secondary Contact No")]
        public string SecondaryContactNo { get; set; }
        [DisplayName("Waste Carrier Ref")]
        public string WasteCarrierRef { get; set; }
        [DisplayName("Unit No")]
        public string UnitNo { get; set; }
        [DisplayName("Street Name")]
        [StringLength(50), Required(ErrorMessage = "Street name required, please enter a street name ")]
        public string StreetName { get; set; }
        [StringLength(50), Required(ErrorMessage = "Town required, please enter a town")]
        public string Town { get; set; }
        [StringLength(50), Required(ErrorMessage = "County is required, please enter a county")]
        public string County { get; set; }
        [StringLength(10), Required(ErrorMessage = "Postcode is required, please enter a postcode")]
        public string Postcode { get; set; }
        public List<CustomerSite> CustomerSites { get; set; }
    }
}

更新:

我仍然不能让它做我需要的,我已经创建了一个包含

的主视图模型
public IEnumerable <Customer> SomeCustomer { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }

那么在我看来,

@model IEnumerable<trsDatabase.Models.masterViewModel>

如果我尝试通过为每个输入使用 a 来访问模型

@foreach (var customer in Model)

当我输入@customer 时。它只会让我访问两个列表,而不是列表中的单个属性。

我认为我可以使用例如@customer.CustomerSites.UnitNo

但我能走的最远的是@customer.CustomerSites

你对我在这里做错了什么有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc razor viewbag


    【解决方案1】:

    我认为你可以做得更好一点。 首先 - 创建一个名为 ViewModel 的新类。将此添加到您的模型文件夹中,将其称为 ex。 CustomerSitesViewModel.cs

    在里面添加一个定义

    公共 IEnumerable 客户 {get;set;} 公共 IEnumerable CustomerSites {get;set;}

    或者您可以创建一个 ViewModel CustomerSiteViewModel.cs,其中包含

    公共客户 CustomerDetails {get;set;} 公共客户站点 CustomerSiteDetails {get;set;}

    您可以将字段命名为 Customer 和 CustomerSite,但我没有这样做是出于演示目的,因此您不会陷入递归循环,不知道要调用哪个字段。

    那么你的观点(查看一个)是

    @model 客户视图视图模型

    或者如果你需要一个列表

    @model IEnumerable

    这是与 ViewBag 数据相反的首选方式。它是强类型的。更容易理解,更不容易出错。

    【讨论】:

      【解决方案2】:

      q 不是一个客户,而是一个客户序列。因此,当您说ViewBag.customer = q; 时,ViewBag.customer 属性变为IEnumerable&lt;Customer&gt; 类型,很自然,它没有CustomerName 属性。

      【讨论】:

      • 感谢您的回复,所以如果我选择一个客户而不是客户列表,那么语法会起作用吗?
      • 无论如何我可以从客户表中提取客户名称列表以包含在客户站点列表中吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      相关资源
      最近更新 更多