whisk
whisk

Reputation: 655

Returning Linq value in controller and passing it to View

Having a problem returning and passing the desired values to the View. I run the LINQ query in LINQPad4 and it returns the correct results. I feel I'm missing something simple here. I've using this post HERE as a reference

Im getting the

CS1061: 'IEnumerable' does not contain a definition for 'FirstName' and no extension method 'FirstName' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)ERROR. \

When I run and step through the code i dont see any values and the message I'm given is

Message = "Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context."

UPDATE

Changing the View from @model IEnumerable<Login.Models.EditProfile> to @model Login.Models.EditProfile Helped with the CS1061 Error I was having and as for the LINQ query not working please look at the accepted answer below by Titian.

Any help would be greatly appreciated. Let me know If there is more information I can provide.

/ Controller /

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);
        }
    }

/Model/

    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; }

    }

/View/

@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")

Upvotes: 2

Views: 1825

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249506

You want to add a FirstOrDefault to your query. Select will return a value (of type IQueryable<User>) even if there are no values (an empty result). You actually want the first value in that result or null if there are no matching results:

        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();

Upvotes: 2

Related Questions