1

I have an EDIT view strongly typed to a model that has 2 fields: Name and category. Name is just a string and category is chosen from a dropdown list.

My controller:

 [HttpGet]
        public ActionResult EditAuthor(int id)
        {
            var db = new AuthorDatacontext();
            var Author = db.Authors.Find(id);
            ViewBag.category = new SelectList(new[] { "ScienceFiction", "fantasy", "LoveStory", "History" });
            return View(Author);
        }

My View:

<div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.category)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.category, (SelectList)ViewBag.category)

            @Html.ValidationMessageFor(model => model.category)
        </div>

Right now the dropDown list just shows all the options that i can choose from but not the option that was already chosen. How i can make it show first the category that was shown already?

2
  • Are you sure that what's in the Model.category property on page load is something that's in the drop down list?
    – mattytommo
    May 8, 2013 at 12:51
  • yes because it is something i can see in the details Page and i choose it from the dropdown list in the create PAge May 8, 2013 at 13:08

1 Answer 1

1

I think it could be down to the fact that you're not setting the IsSelected property. Try this instead:

First, let's make a view model so we can put the dropdown in there:

public class AuthorViewModel
{
    public Author Author { get; set; }
    public List<SelectListItem> Categories { get; set; }
}

Then in your controller method, let's populate your model:

[HttpGet]
public ActionResult EditAuthor(int id)
{
    var db = new AuthorDatacontext();
    var selections = new List<string> { "ScienceFiction", "fantasy", "LoveStory", "History" };
    var model = new AuthorViewModel();

    model.Author = db.Authors.Find(id);
    model.Categories = selections
    .Select(s => new SelectListItem
                 {
                     Text = s,
                     Value = s,
                     Selected = s == model.Author.Category
                 })
    .ToList();

    return View(model);
}

Then change your view model type:

@model AuthorViewModel

Then you can do:

@Html.DropDownListFor(model => model.Author.Category, Model.Categories)
5
  • i didnt understand where to put this code. var selection supposed to be in the controller, but it doesnt have model.category (maybe you mean author.category). Anyway, the problem is that drop down get a SelectList type and not a List<string> type... May 8, 2013 at 13:23
  • @AlexandraOrlov Put that code in your controller, yeah you're right sorry I've changed it to author. Check my edit, I've added you the razor code for your view too.
    – mattytommo
    May 8, 2013 at 13:25
  • The problem with the line that you wrote in the View is that it needs to be casted to a specific type. And it cant be casted to (List<string>) May 8, 2013 at 14:18
  • @AlexandraOrlov That's a problem with using the ViewBag. Check my update, I've put it all in a model for you :)
    – mattytommo
    May 8, 2013 at 14:25
  • ok, now its totally different :) Before i had String category. I will check it out, thanks May 9, 2013 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.