使用 viewbag 将列表返回到强类型视图
我正在尝试使用 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>
如果我尝试通过键入每个
@foreach (var customer in Model)
当我输入 @customer 来访问模型。它只会让我访问两个列表,而不是列表中的各个属性。
我以为我可以用@customer.CustomerSites.UnitNo 来做到这一点
,但我能做的最远的是@customer.CustomerSites
你对我在这里做错了什么有什么想法吗?
I am trying to return a list of customers to a strongly typed view using the viewbag and am having some difficulties as im new to MVC and Razor, can anyone kindly offer any advice on the following problem please?
I have the following Code in my controller,
public ViewResult Index()
{
var q = from a in db.Customers
select a;
ViewBag.customer = q;
return View(db.CustomerSites.ToList());
}
and this code in my view
@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>
}
So i am retrieving a list of customers and customer sites, the sites are typed to the view,
@model IEnumerable<trsDatabase.Models.CustomerSite>
I am trying extract CustomerName from the list of customers using
@ViewBag.customer.CustomerName
But this is generating an error saying the "Customer" type doesnt contain a defintion for customer name but it does as shown below so im not sure why the error is occuring.
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; }
}
}
Update:
I still cant get this to do what I need, I have created a master view model that includes
public IEnumerable <Customer> SomeCustomer { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
Then in my view,
@model IEnumerable<trsDatabase.Models.masterViewModel>
If i try to access the model by typing using a for each
@foreach (var customer in Model)
When I type @customer. It will only let me access the two lists not the individual properties in the list.
I thought I would have been able to do this with e.g. @customer.CustomerSites.UnitNo
But the furthest I can go is @customer.CustomerSites
Do you have any ideas on what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你可以让这件事变得更好一点。
首先 - 创建一个名为 ViewModel 的新类。将其添加到您的模型文件夹中,将其称为 ex。 CustomerSitesViewModel.cs
在其中添加一个定义
或者您可以创建一个 ViewModel CustomerSiteViewModel.cs,其中包含
您可以将字段命名为 Customer 和 CustomerSite 但我没有这样做用于演示目的,这样您就不会陷入递归循环不知道该打电话给哪一个。
那么您的视图(查看一个)是
或者如果您需要一个列表,
这是与 ViewBag 数据相反的首选方式。它是强类型的。更容易遵循,并且不易出错。
I think you can make this a bit better.
First - create a new class called a ViewModel. Add this to your models folder, call it for ex. CustomerSitesViewModel.cs
In it, add a definition for
Or you can create a ViewModel CustomerSiteViewModel.cs which contains
You could name the fields Customer and CustomerSite but I didn't do that for demo purposes so you don't get stuck in a recursive loop not realizing which one to call.
Then your view (to view one) is
or if you need a list
This is the preferred way as opposed to ViewBag data. It's strongly typed. easier to follow, and less error prone.
q
不是一个客户,而是一个客户序列。因此,当您说ViewBag.customer = q;
时,ViewBag.customer
属性将变为IEnumerable
类型,这很自然,没有CustomerName
属性。q
is not a customer, but a sequence of customers. Therefore, when you sayViewBag.customer = q;
, theViewBag.customer
property becomes of typeIEnumerable<Customer>
, which, quite naturally, does not have aCustomerName
property.