ASP/MVC3 Razor Linq [英] ASP/MVC3 Razor Linq

查看:57
本文介绍了ASP/MVC3 Razor Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目不断出现此错误.

I keep getting this error for my project.

传递到字典中的模型项是类型 'System.Collections.Generic.List 1[<>f__AnonymousType2 2 [System.String,System.String]]', 但是此字典需要类型为的模型项 'System.Collections.Generic.IEnumerable`1 [ETMS.Models.DB.tblParent]'.

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType22[System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ETMS.Models.DB.tblParent]'.

描述:执行以下操作时发生未处理的异常 当前的Web请求.请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源的信息.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

================================================ ==================================

==================================================================================

我想做的是从数据库中检索数据(一对多),这是我的数据库表结构

What i want to do is to retrieve the data from the database (one to many) , This is my database table structure

tblparent
- Parent_ID
- Username
- Password
- Firstname
- Lastname

tblParentEmail
- ParentEmail_ID
- Email
- Parent_ID

我被定为从电子邮件到父母的外交关系,但是当出现另一个错误时,我无法包含在EF中.我这样做,并导致我这个错误:

i was made the foreign relation from email to parent, but i could not include with EF while there is another error. i do in this way and caused me this error :

public ActionResult Clientlist()
{
    using (ETMSPeopleEntities db = new ETMSPeopleEntities())
    {
        //var sxc = db.tblParents.Include("tblLocation").Include("tblParentEmails.ParentEmail_ID")
        //    .OrderByDescending(p => p.Status).ToList();
        var members = (from x in db.tblParentEmails
                      join y in db.tblParents
                      on x.Parent_ID equals y.Parent_ID 
                      select new { Email = x.ParentEmail, UserName = y.Username }).AsEnumerable();
        return View(members.ToList());
    }
}

这是我的管理员

@model IEnumerable<ETMS.Models.DB.tblParent>
@{
    ViewBag.Title = "Clientlist";
}

<h2>Clientlist</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Username
        </th> 
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr> 
        <td>
            @Html.DisplayFor(modelItem => item.Username)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.Firstname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Lastname)
        </td>
          <td>
            @Html.DisplayFor(modelItem => item.Location_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreateTime)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Parent_ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.Parent_ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Parent_ID })
        </td>
    </tr>
}

</table>

这是客户列表视图

推荐答案

您要将匿名对象列表传递给视图:

You're passing a list of anonymous objects to your view:

select new { Email = x.ParentEmail, UserName = y.Username }

该视图需要IEnumerable<ETMS.Models.DB.tblParent>:

@model IEnumerable<ETMS.Models.DB.tblParent>

您应该将选择更改为:

select y

以使代码正常工作.

这是您可以使用视图模型模式的方式.首先,创建一个视图模型类,这样就不会向视图传递匿名类型.我们将其称为AwesomeEmailViewModel,看起来您需要.Email.Username和其他一些属性,因此我们也将对其进行设置.

Here is how you could use a view model pattern. First, create a view model class, so you're not passing an anonymous type to your view. Let's call it AwesomeEmailViewModel, and it looks like you need .Email, .Username and some other properties, so we'll set those up too.

public class AwesomeEmailViewModel
{
    public string Email { get; set; }
    public string Username { get; set; }
    public string FirstName{ get; set; }
    public string LastName { get; set; }
    public int Location_ID{ get; set; }
    public DateTime CreateTime { get; set; }
}

现在,通过使用对象初始化来填充AwesomeEmailViewModel

Now, modify your query by using object initialization to populate an instance of AwesomeEmailViewModel

注意:我正在猜测哪些属性属于哪些对象(tblParent或tblParentEmails,因此您需要仔细检查这些

Note: I am guessing which properties belong to which objects (either tblParent or tblParentEmails, so you will need to double-check these

var members = (from x in db.tblParentEmails
               join y in db.tblParents
               on x.Parent_ID equals y.Parent_ID 
               select new AwesomeEmailViewModel()
               { 
                   Email = x.ParentEmail, 
                   UserName = y.Username,
                   FirstName = y.FirstName,
                   LastName = y.LastName,
                   Location_ID = x.Location_ID,
                   CreateTime = y.CreateTime, 
               }).ToList();
               // I don't know if you'll need the `AsEnumerable()` call

return View(members);

最后,您的视图必须知道期望的类型,所以让我们对其进行修改以期望我们新创建的AwesomeEmailViewModel实例的列表.

Finally, your view has to know what type(s) to expect, so let's modify it to expect a list of our newly created AwesomeEmailViewModel instances.

@model IEnumerable<ETMS.Models.AwesomeEmailViewModel>

请密切注意,就像我在命名空间中所猜到的那样.无论如何,这应该使您能够访问视图内所需的属性.如果需要更多功能,则需要修改我们创建的新视图模型类以及控制器操作中的查询.

Pay close attention, as I guessed at the namespace as well. In any case, this should give you access to the properties you need inside your view. If you need more, you'll need to modify the new view model class we created as well as the query in your controller action.

这篇关于ASP/MVC3 Razor Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆