C# 在控制器中返回Linq值并将其传递给视图

C# 在控制器中返回Linq值并将其传递给视图,c#,asp.net-mvc,entity-framework,linq,C#,Asp.net Mvc,Entity Framework,Linq,返回所需值并将其传递给视图时出现问题。我在LINQPad4中运行LINQ查询,它返回正确的结果。我觉得我错过了一些简单的东西。我用这篇文章作为参考 我收到通知了 CS1061:“IEnumerable”不包含“FirstName”的定义,并且找不到接受“IEnumerable”类型的第一个参数的扩展方法“FirstName”(是否缺少using指令或程序集引用?)错误。 \ 当我运行并单步执行代码时,我看不到任何值,并且给出的消息是 Message=“无法创建“System.Object”类型的

返回所需值并将其传递给视图时出现问题。我在LINQPad4中运行LINQ查询,它返回正确的结果。我觉得我错过了一些简单的东西。我用这篇文章作为参考

我收到通知了

CS1061:“IEnumerable”不包含“FirstName”的定义,并且找不到接受“IEnumerable”类型的第一个参数的扩展方法“FirstName”(是否缺少using指令或程序集引用?)错误。 \

当我运行并单步执行代码时,我看不到任何值,并且给出的消息是

Message=“无法创建“System.Object”类型的常量值。在此上下文中仅支持基元类型或枚举类型。”

更新

将视图从
@model IEnumerable
更改为
@model Login.Models.EditProfile
有助于解决我遇到的CS1061错误,至于LINQ查询不起作用,请查看下面Titian接受的答案

任何帮助都将不胜感激。 如果我能提供更多信息,请告诉我

/控制器/

public ActionResult Edit(int? id)
    {


        using (TDBEntities db1 = new TDBEntities())
        {
            var user =  (from a in db1.ExternalUsers
                        join b in db1.ExternalUserEmails on a.ExternalUserID equals b.ExternalUserID
                        join c in db1.ExternalUserPhones on a.ExternalUserID equals c.ExternalUserID
                        where a.ExternalUserID.Equals(id)
                        select new EditProfile {ExternalUserID = a.ExternalUserID, FirstName = a.FirstName, LastName = a.LastName, EmailAddress = b.EmailAddress, PhoneNumber = c.PhoneNumber });

                if (user == null)
                {
                    return HttpNotFound();
                }

            return View(user);
        }
    }
/型号/

    public partial class EditProfile
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string EmailAddress { get; set; }
      public string PhoneNumber { get; set; }
      public int ExternalUserID { get; set; }

    }
@model IEnumerable<Login.Models.EditProfile>
@using Login.Helpers

@{
 ViewBag.Title = "Update Employee";
 }

 <h2></h2>

  @using (Html.BeginForm())
 {
   @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Update Employee</h4>
    <hr />

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, "First Name:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, "Last Name:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </div>
    </div>    

    <div class="form-group">
        @Html.LabelFor(model => model.EmailAddress, "Email Address:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PhoneNumber, "Phone Number:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="resetPw control-label col-md-2 ">Reset Password</label>            
        <div> @Html.CheckBox("ResetPassword", false, new { @style = "margin: 10px 15px 0;" }) <i>(check to reset)</i></div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
/查看/

    public partial class EditProfile
    {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string EmailAddress { get; set; }
      public string PhoneNumber { get; set; }
      public int ExternalUserID { get; set; }

    }
@model IEnumerable<Login.Models.EditProfile>
@using Login.Helpers

@{
 ViewBag.Title = "Update Employee";
 }

 <h2></h2>

  @using (Html.BeginForm())
 {
   @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Update Employee</h4>
    <hr />

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, "First Name:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, "Last Name:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </div>
    </div>    

    <div class="form-group">
        @Html.LabelFor(model => model.EmailAddress, "Email Address:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PhoneNumber, "Phone Number:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="resetPw control-label col-md-2 ">Reset Password</label>            
        <div> @Html.CheckBox("ResetPassword", false, new { @style = "margin: 10px 15px 0;" }) <i>(check to reset)</i></div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
@model IEnumerable
@使用Login.Helpers
@{
ViewBag.Title=“更新员工”;
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
更新员工

@LabelFor(model=>model.FirstName,“FirstName:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”}) @LabelFor(model=>model.LastName,“LastName:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”}) @LabelFor(model=>model.EmailAddress,“电子邮件地址:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.EmailAddress,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.EmailAddress,“,new{@class=“text danger”}) @LabelFor(model=>model.PhoneNumber,“电话号码:”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.PhoneNumber,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.PhoneNumber,“,new{@class=“text danger”}) 重置密码 @复选框(“ResetPassword”,false,新的{@style=“margin:10px 15px 0;”})(选中以重置) } @ActionLink(“返回列表”、“索引”)
您想在查询中添加
FirstOrDefault
。即使没有值(空结果),Select也将返回一个值(类型为
IQueryable
)。实际上,您需要该结果中的第一个值,如果没有匹配的结果,则需要null:

        var user =  (from a in db1.ExternalUsers
                    join b in db1.ExternalUserEmails on a.ExternalUserID equals b.ExternalUserID
                    join c in db1.ExternalUserPhones on a.ExternalUserID equals c.ExternalUserID
                    where a.ExternalUserID == id
                    select new EditProfile {ExternalUserID = a.ExternalUserID, FirstName = a.FirstName, LastName = a.LastName, EmailAddress = b.EmailAddress, PhoneNumber = c.PhoneNumber })
                    .FirstOrDefault();

如果您正在编辑一条记录,您的视图应强类型为
@model Login.Models.EditProfile
。此外,您还需要从action方法发送单个对象来查看,而不是一个集合。添加
.FirstOrDefault()时使用
FirstOrDefault()
我获取System.NotSupportedException:'无法创建'System.Object'类型的常量值'。在此上下文中仅支持基元类型或枚举类型。请使用
=
而不是
Equals
以实现id相等