0

I am playing with MVC 4 in VS 2012 with a local DB using Entity Framework.

On one of my edit pages for customer details I have a drop down box that displays the customer's surname. The razor code for this is

    <div class="editor-label">
        @Html.LabelFor(model => model.CustomerID, "Customer")
    </div>
    <div class="editor-field">
        @Html.DropDownList("CustomerID", String.Empty)
        @Html.ValidationMessageFor(model => model.CustomerID)
    </div>

My customer DB table has the following setup.

    [CustomerID]   INT            IDENTITY (1, 1) NOT NULL,
    [LastName]     NVARCHAR (MAX) NULL,
    [FirstMidName] NVARCHAR (MAX) NULL,
    [JoinDate]     DATETIME       NOT NULL,
    [Email]        NVARCHAR (MAX) NULL,
    [Address]      NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.Customer] PRIMARY KEY CLUSTERED ([CustomerID] ASC));

How do I combine both firstmidname and last name in the drop down?

1
  • Usually you will have to create view models to pass data between the view and the controller. So this is also an option. Also, if you don't want to go with this or the T.Rahgooy answer you can try Konrad Gadzina answer here - stackoverflow.com/questions/16099258/…. Pretty sure that if you concatenate the three strings it will work. Commented Mar 25, 2014 at 20:51

3 Answers 3

2

Use a View Model for this and store your SelectList as a property of the View Model:

public ViewModel 
{
    public int CustomerID { get; set; }
    public IList<SelectListItem> Customers { get; set; }

    public ViewModel() {
        this.Customers = new List<SelectListItem>();
    }
}

In your controller populate the Customers select list:

var vm = new ViewModel();

vm.Customers = _customerService.All()
                   .Select(x => new SelectListItem {
                       Value = x.CustomerID.ToString(),
                       Text = x.FirstMidName + " " + x.LastName
                   })
                   .ToList();

return View(vm);

and in your view:

@model ViewModel

@Html.DropDownListFor(x => x.CustomerID, Model.Customers, "Please select a customer...") 
Sign up to request clarification or add additional context in comments.

Comments

2

In controller:

ViewBag.CustomerID= new SelectList((from s in db.customer.ToList()
                               select new
                                  {
                                    CustomerID= s.CustomerID,
                                    FullName = s.LastName+ " " + s.FirstMidName
                                  }),
       "CustomerID",
       "FullName",
       null);

In view:

 @Html.DropDownList("CustomerID", null, htmlAttributes: new { @class = "form-control" })

Comments

0

You can create a property in the customer class that is not mapped to the database like this

public string FullName{ get{ return FirstMidName + " " + LastName;}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.