返回控制器中的Linq值并将其传递给View

时间:2017-11-22 14:37:22

标签: c# asp.net-mvc entity-framework linq

返回问题并将所需的值传递给View。我在LINQPad4中运行LINQ查询,它返回正确的结果。我觉得我在这里错过了一些简单的东西。我已将此帖HERE用作参考

我得到了

  

CS1061:' IEnumerable'不包含' FirstName'的定义没有扩展方法' FirstName'接受类型' IEnumerable'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)错误。   \

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

  

消息="无法创建类型' System.Object'的常量值。在此上下文中仅支持原始类型或枚举类型。"

更新

将视图从@model IEnumerable<Login.Models.EditProfile>更改为@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")

1 个答案:

答案 0 :(得分:2)

您想在查询中添加java.sql.SQLException: ORA-06553: PLS-306: wrong number or types of arguments in call to 'CAST_TO_VARCHAR2' 。即使没有值(空结果),Select也会返回一个值(FirstOrDefault类型)。您实际上想要该结果中的第一个值,如果没有匹配结果,则为null:

IQueryable<User>