4

I'm brand new to ASP.NET MVC, and I would appreciate any help with my question. I already did plenty of research (not enough apparently) on this topic. I need to bind a dropdownlist to a specific column in a table and then render it in the view. I already have the query to retrieve the table in the controller:

  public ActionResult SelectAccountEmail()
        {
            var queryAccountEmail = (from AccountEmail in db.UserBases select AccountEmail) 
            var selectItems = new SelectList(queryAccountEmail);
            return View(selectItems); 
        }

I get lost when it come to binding the query to a dropdownlist in the view.

@model RecordUploaderMVC4.Models.UserBase

@{
    ViewBag.Title = "SelectAccountEmail";
}

<h2>SelectAccountEmail</h2>

@Html.LabelFor(model => model.AccountEmail); 

@Html.DropDownList(Model.AccountEmail);  
@Html.ValidationMessageFor(model => model.AccountEmail); 

<input /type="submit" value="Submit">

I get this error when I run it:

Server Error in '/' Application.
--------------------------------------------------------------------------------


The model item passed into the dictionary is of type 'System.Web.Mvc.SelectList', but this dictionary requires a model item of type 'RecordUploaderMVC4.Models.UserBase'. 

Any help will be appreciated.

Thanks in advance.

1
  • your view is expecting RecordUploaderMVC4.Models.UserBase as model and you are passing an object of type SelectList. Provide appropriate type of model object. Commented May 8, 2013 at 14:45

2 Answers 2

3

Few things wrong. Firstly, change your model to add the following properties (Looking at your view, it's RecordUploaderMVC4.Models.UserBase):

public class UserBase
{
    public string AccountEmail { get; set; }
    public SelectList Emails { get; set; }
    //rest of your model
}

Then, build your model in your controller properly:

 public ActionResult SelectAccountEmail()
 {
     UserBase model = new UserBase();
     var queryAccountEmail = (from AccountEmail in db.UserBases select AccountEmail) 
     model.Emails = new SelectList(queryAccountEmail);

     return View(model); 
 }

Then in your view you can do:

@Html.LabelFor(model => model.AccountEmail)

@Html.DropDownListFor(model => model.AccountEmail, Model.Emails)
@Html.ValidationMessageFor(model => model.AccountEmail)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you quick response mattytommo! I'm actually using Entity Framework 5, and I reverse engineer code first the database I'm using, and then I added an ADO.NET Entity Data Model so that I can use Stored Procedures as reverse engineer code first didn't provide the use of sprocs. Long story short the UserBase model was generated by entity framework. Therefore, are you suggesting to modify it by adding the public SelectList Emails line?
I wish this type of example would be the first part of every MVC tutorial from MS. Not sure why there's so much focus on the (terrible) ViewData/Bag stuff, and on controllers doing all the work.
0

Step 1: First Create a model Like this to hold your ListofAccountEmail

public class AccountEmailViewModel
    {

        public int AccountEmailId { get; set; }
        public string AccountEmailDescription { get; set; }
    }

Step 2: Create your model class

 public class UserBaseViewModel
    {    


                public IEnumerable<SelectListItem>  AccountEmail { get; set; }
                public string AccountEmail { get; set; }
    }

Step 3 :

In Controller

            [HttppGet]
            public ActionResult SelectAccountEmail()
            {
                var EmailAccounts = (from AccountEmail in db.UserBases select AccountEmail)

               UserBase userbaseViewModel = new UserBase
                {
                    AccountEmail = EmailAccounts.Select(x => new SelectListItem
                  {
                      Text = x.AccountEmailDescription,
                      Value = Convert.ToString(x.AccountEmailId)
                  }).ToList()

                };
                return View(userbaseViewModel);
            }

Step 4 : In View

 @model RecordUploaderMVC4.Models.UserBase
    @{
       ViewBag.Title = "SelectAccountEmail";
    }
    <h2>SelectAccountEmail</h2>
    @Html.ValidationSummary()
    <h2>SelectAccountEmail</h2>
                            @Html.LabelFor(model => model.AccountEmail )

                                @Html.DropDownListFor(x => x.AccountEmailId, Model.AccountEmail, "Please Select", "")
                            </div>
    <input /type="submit" value="Submit">

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.