[英]MVC 4 with EF5 - Model View - code in wrong places?
I normally work with SQL and SSRS and am new to C# and working on my first MVC project. 我通常使用SQL和SSRS,并且是C#的新手,并且从事我的第一个MVC项目。
My objective was to create page displaying a table with parent and their associated child records - without the parents repeating for each child value. 我的目标是创建一个显示带有父母及其相关子记录的表格的页面-父母不为每个子值重复。
Column1 Primary Key from parent table 父表的Column1主键
Column 2 Name from parent table 列2父表的名称
Column 3 List of child names 第3列子名称列表
To accomplish this I attempted to create a Model View. 为此,我尝试创建一个模型视图。
I believe I have made a number of mistakes related to placement of code 我相信我犯了许多与代码放置有关的错误
Model View 模型视图
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace SupplierItemTest.Models
{
public class VM_Trade
{
[Required]
[Display(Name = "Tax Info")]
public virtual string TaxpayerID { get; set; }
[Required]
[Display(Name = "Entity Name")]
public virtual string Supplier { get; set; }
[Required]
[Display(Name = "Trading Names")]
public virtual string SupplierTradingName1 { get; set; }
[Required]
[Display(Name = "Supplier Number")]
public virtual string EhSupplierNumber { get; set; }
}
}
Controller View 控制器视图
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SupplierItemTest.Models;
namespace SupplierItemTest.Controllers
{
public class TradingNameController : Controller
{
private SupplierItemEntities db = new SupplierItemEntities();
//
// GET: /TradingName/
public ActionResult Index()
{
var Trade_View =
from s in db.Suppliers
join st in db.SupplierTradingNames on s.TaxpayerID equals st.TaxpayerID
join sn in db.EhSuppliers on s.TaxpayerID equals sn.TaxpayerID
orderby s.TaxpayerID
select new VM_Trade
{
SupplierTradingName1 = st.SupplierTradingName1,
TaxpayerID = s.TaxpayerID,
Supplier = s.SupplierName,
EhSupplierNumber = sn.EhSupplierNumber
};
return View(Trade_View.ToList());
View 视图
@model IEnumerable<SupplierItemTest.Models.VM_Trade>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.TaxpayerID)
</th>
<th>
@Html.DisplayNameFor(model => model.Supplier)
</th>
<th>
@Html.DisplayNameFor(model => model.EhSupplierNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.SupplierTradingName1)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TaxpayerID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier)
</td>
<td>
@Html.DisplayFor(modelItem => item.EhSupplierNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.SupplierTradingName1)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
Error "Cannot implicitly convert type 'string' to 'Systen.Collections.Generic.IEnumerable 错误“无法将类型'string'隐式转换为'Systen.Collections.Generic.IEnumerable
I was hoping some could tell me: 我希望有人能告诉我:
From what I have read I have the following very wrong: 根据我的阅读,我有以下错误:
I have been reading for a couple of day and just can seem to get anything to work. 我已经阅读了几天,似乎什么都可以工作。
In your View you fist access your model like it be an object then few lines below you are trying to enumerate it. 在“视图”中,您可以像对待对象一样直接访问模型,然后尝试枚举下面的几行。
So I suggest you following 所以我建议你关注
在select new VM_Trade
之后添加(),直到创建VM_Trade
类的新对象VM_Trade
。
There are problems with your view model and view. 您的视图模型和视图存在问题。 You can reference the basic structure for a similar situation here . 您可以在此处参考类似情况的基本结构。
I would also suggest you to have a look at Music Store application tutorial which explain these concept very well. 我还建议您看一下Music Store应用程序教程,该教程很好地解释了这些概念。
You are on the right path, just few changes in the ViewModel and View, and you will be done. 您走在正确的道路上,只需对ViewModel和View进行少量更改即可完成。 Once the structure is fixed, you will have more specific question that the community will be happy to answer. 固定结构之后,您将有一个更具体的问题,社区将很乐意回答。
EDIT: Just a bit of help, there is this Nuget package "EntityFramework.Sample" which give you sample models and a demo DbContext set to play around. 编辑:只是有点帮助,这里有这个Nuget包“ EntityFramework.Sample”,它为您提供了示例模型和一个演示DbContext集,供您试用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.