2

I cannot link dropdownlist to my model in the view. I have the error message: The ViewData item that has the key 'title' is of type 'System.String' but must be of type 'IEnumerable with the following code:

public class FareCustomer
{
    [Required]
    public int title { get; set; }

My controller:

List<SelectListItem> titleList = new List<SelectListItem>();
titleList.Add(new SelectListItem { Text = "Non renseigné", Value = "0" });
titleList.Add(new SelectListItem { Text = "Monsieur", Value = "1" });
titleList.Add(new SelectListItem { Text = "Madame", Value = "2" });
titleList.Add(new SelectListItem { Text = "Mademoiselle", Value = "3" });
ViewBag.title = titleList;
//Create a new customer
FareCustomer fareCustomer = new FareCustomer();
fareCustomer.title = 1;
return View("CreateAccount", fareCustomer);

My view 'CreateAccount'

@model PocFareWebNet.Models.FareCustomer
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    @Html.DropDownList("title")
    <input type="submit" value="Save" />
</fieldset>
}

I tried to apply what is described here: Using the DropDownList Helper with ASP.NET MVC but obviously I missed something.

1
  • 1
    Did you get the error after posting the form? Commented Nov 8, 2013 at 15:38

3 Answers 3

2

Try casting to IEnumarable and use model based helper

@Html.DropDownListFor(m => m.title,((IEnumerable<SelectListItem>)ViewBag.title))
Sign up to request clarification or add additional context in comments.

Comments

2

I found my issue. I used the view generated by Visual Studio and it already defines ViewBag.Title

@model PocFareWebNet.Models.FareCustomer
@{
   ViewBag.Title = "CreateAccount";
}

This is why the error says the type is string and cannot be converted to IEnumerable.

It seems razor is not case sensitive: The controller set ViewBag.title but the view set ViewBag.Title and overwrites ViewBag.title coming from the controller.

Thank you for your help.

Comments

1

Try this instead :

@Html.DropDownListFor(model => model.title, (SelectList)ViewBag.title)

The first argument will be receiving your selected value.

3 Comments

The error is now: Cannot convert type 'string' to 'System.Web.Mvc.SelectList'
It seems that your ViewBag.title is not setted as your List<SelectListItem>, can you show more code on the controller side (both GET and POST actions) ?
There's another possibility : ViewBag.Title is probably used in your layout or a parent view, as the title of the page, and there's a chance that your List<SelectListItem> gets overriden somewhere before getting to your drop down list. You can either search for other usages of ViewBag.Title, or rename "Title" to something more specific, like ViewBag.FareCustomerTitleList. You can also verify it by displaying ViewBag.title's current value in your view.

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.