两个或更多实体模型和一个视图

时间:2013-12-26 19:51:25

标签: asp.net-mvc entity-framework asp.net-mvc-4

我是MVC4的新手。我试图从连接查询中获取一行,我想在视图中显示数据,但是出现错误。我不确定实际问题在哪里。此LINQ查询返回单行。

控制器

public ActionResult Index()
    {
        var userId = WebSecurity.GetUserId(User.Identity.Name);
        var doctorProfile =
           ( from DP in db.DoctorProfiles
            from PP in db.ProfilePictures
            where
              PP.UserProfiles.UserId == 13
            select new
            {
                DP.DoctorProfileId,
                DP.UserId,
                DP.UserName,
                DP.DocName,
                DP.ForeignDegree,
                DP.MbbsPassingYear,
                DP.SocietyMember,
                DP.NationalId,
                DP.CurrentRisingPlace,
                DP.Email,
                DP.DoctorSpecialistId,
                DP.IsVerified,
                DP.FCPS,
                DP.CellNo,
                DP.DoctorSpecialistName.DoctorSpecialistName,
                PP.ProfilePictureId,
                PP.FullImageUrl,
                PP.ThumbImageUrl,
                PP.AlternateText,
                Expr2 = PP.UserId,
                PP.SmallThumbImageUrl
            }).FirstOrDefault();

        return View(doctorProfile);
    }

并查看

        @model IEnumerable<EPrescription.Models.DoctorProfile>

@{
    ViewBag.Title = "Index";
}
<div class="row">

    <div class="col-lg-8">

        <div class="editProfile">
            <p>UserName : @Html.DisplayFor(m => m.UserName)</p>
            <p>Full Name : @Html.DisplayFor(m => m.DocName)</p>
            <p>FCPS : @Html.DisplayFor(m => m.FCPS)</p>
            <p>Blood Group : @Html.DisplayFor(m => m.DoctorSpecialistName.DoctorSpecialistName)</p>
            <p>Specialis : @Html.DisplayFor(m => m.MbbsPassingYear)</p>
            <p>Society Member : @Html.DisplayFor(m => m.SocietyMember)</p>
            <p>Foreign Degree : @Html.DisplayFor(m => m.ForeignDegree)</p>
            <p>Current Rising Place : @Html.DisplayFor(m => m.CurrentRisingPlace)</p>
            <p>National Id : @Html.DisplayFor(m => m.NationalId)</p>
            <p>Email : @Html.DisplayFor(m => m.Email)</p>
            <p>Cell No : @Html.DisplayFor(m => m.CellNo)</p>
            <p>Is Verified : @Html.DisplayFor(m => m.IsVerified)</p>

        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

您创建并传递匿名类型,但查看接收PatientProfile

更改为:

  var userId = WebSecurity.GetUserId(User.Identity.Name);
        var doctorProfile =
           ( from DP in db.DoctorProfiles
            from PP in db.ProfilePictures
            where
              PP.UserProfiles.UserId == 13
            select new PatientProfile()
            {
              DoctorProfileId =  DP.DoctorProfileId,
              UserId =  DP.UserId,
              UserName =  DP.UserName             
              ...
            });

        return View(doctorProfile);