返回一个列表,使用viewbag强类型视图 [英] Returning a list to strongly typed view using viewbag
问题描述
我试图用viewbag客户名单返回一个强类型的视图和我有一些新的MVC和剃刀困难作为即时通讯,任何人都可以提供亲切对下列问题有什么建议吗?
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?
我有以下的code在我的控制器,
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());
}
这code在我看来
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
但是,这是产生错误称客户类型犯规包含客户名称的确定指标,但它确实如下所示这样的IM不知道为什么错误正在发生。
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; }
然后在我看来,
@model IEnumerable<trsDatabase.Models.masterViewModel>
如果我试图通过使用每个输入访问模型
If i try to access the model by typing using a for each
@foreach (var customer in Model)
当我键入@customer。它只会让我访问两个列表不在列表中的单个属性。
When I type @customer. It will only let me access the two lists not the individual properties in the list.
我想我已经能够例如做到这一点@ customer.CustomerSites.UnitNo
I thought I would have been able to do this with e.g. @customer.CustomerSites.UnitNo
不过,我可以去最远的是@ customer.CustomerSites
But the furthest I can go is @customer.CustomerSites
你对我做错了什么在这里的任何想法?
Do you have any ideas on what I'm doing wrong here?
推荐答案
我觉得你可以让这个好一点。
首先 - 创建一个名为一个ViewModel新类。添加到您的模型文件夹,把它为前。 CustomerSitesViewModel.cs
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
在它,添加一个定义
public IEnumerable Customers {get;set;}
public IEnumerable CustomerSites {get;set;}
或者你可以创建一个视图模型CustomerSiteViewModel.cs包含
Or you can create a ViewModel CustomerSiteViewModel.cs which contains
public Customer CustomerDetails {get;set;}
public CustomerSite CustomerSiteDetails {get;set;}
您能说出领域的客户和CustomerSite但我没有这样做,对于演示目的,所以你不要陷在一个递归循环没有意识到调用哪一个。
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
@model CustomerViewViewModel
或者如果你需要一个名单
or if you need a list
@model IEnumerable
这是相对于ViewBag数据preferred方式。它是强类型的。更容易执行,并减少错误。
This is the preferred way as opposed to ViewBag data. It's strongly typed. easier to follow, and less error prone.
这篇关于返回一个列表,使用viewbag强类型视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!