views:

34

answers:

1

Im using asp.net mvc 2. I have a model Supermodel that consists of 2 models TestModel1 and TestModel2. In SuperModelView Im doing the following thing:

 <%: Html.DisplayFor(x=> x.TestModel1, "TestModel1Template") %>

Its working just fine, except for the fact, that dropdownlist is populated but selected value is not set. Im using the following code for a dropdownlist in my template:

<%: Html.DropDownListFor(x=> x.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.Property1) %>

and its not setting the selected property. I put the code below to SuperModelView, that calls <%: Html.DisplayFor To populate the template and it works just fine. So I`m kinda puzzled, what is the difference?

<%: Html.DropDownListFor(x=> x.TestModel1.Property1, (IEnumerable<SelectListItem>)ViewData["MyDDLList"], Model.TestModel1.Property1) %>

UPDATE: I`ve tried to investigate the issue, but something is totally wrong. I can share the whole code, not sure where to put it, here or attach with separate files.

@Darin, what other parts should I share, or just share the whole model view and controller files?

A: 

Firstly display templates are just for displaying. If you need to edit with drop down use editor template:

<%: Html.EditorFor(x => x.TestModel1, "TestModel1Template") %>

and in your editor template:

<%: Html.DropDownListFor(x => x.Property1, Model.MyDDLList) %>

where MyDDLList is defined like:

public IEnumerable<SelectListItem> MyDDLList { get; set; }

and in your controller action you fill the values:

public ActionResult Foo() 
{
    var model = new SuperViewModel 
    {
        TestModel1 = new TestModel1
        {
            // Set some selected value
            Property1 = "1",

            // Fill the drop down values
            // TODO: use a repository
            MyDDLList = new SelectList(new[]
            {
                new SelectListItem { Value = "1", Text = "text 1" },
                new SelectListItem { Value = "2", Text = "text 2" },
                new SelectListItem { Value = "3", Text = "text 3" },
            }, "Value", "Text")
        }
    }
    return View(model);
}

UPDATE:

Here's a complete working example:

Model:

public class MyViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Controller:

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // Preselect the second item
            SelectedItemId = "2",
            Items = new SelectList(new[]
            {
                new SelectListItem { Value = "1", Text = "item 1" },
                new SelectListItem { Value = "2", Text = "item 2" },
                new SelectListItem { Value = "3", Text = "item 3" },
            }, "Value", "Text")
        };
        return View(model);
    }
}

View (~/Views/Index.aspx):

<%: Html.DisplayForModel() %>

DisplayTemplate (~/Views/DisplayTemplates/MyViewModel):

<%: Html.DropDownListFor(x => x.SelectedItemId, Model.Items) %>
Darin Dimitrov
Sorry, didn`t get it, it`s not working because I made a DisplayTemplate? I know how to make Edit template, I do not need it, I need display with dropdownlists, maybe will disable them.
Yaroslav Yakovlev
Any reason for the downvote? Please leave a comment when downvoting explaining why do you think this answer is wrong.
Darin Dimitrov
Darin, I`ve wrote you but didn`t downvoted. As you didn`t answer and your response is not an answer I downvoted ...
Yaroslav Yakovlev
Isn't my answer working for you? I've offered you an alternative which avoids using ViewData and type casting in your view. I've also recommended you using Editor templates instead of Display templates because a drop down is for editing data, not displaying, but you could use display templates if you want, that's not the problem.
Darin Dimitrov
Darin, Ive asked for the different thing, please, read my question and comment above. I guess your code will work, but the question is not to make dropdownlist. I can make it with other things. The question is why it`s not working in this case Ive put in the question. Just read your update. Read my question please, the same code works fine in other place. And answer do not use display template seems not an answer :-). I`m trying to use display template, it`s not wokring ...
Yaroslav Yakovlev
@Yaroslav, what you've put in your question is a code fragment, not a complete example. How can you expect someone telling what is wrong from the information you provided? There are 3 letters in MVC and unless you provide complete example with your Model, Controller and View allowing people to reproduce the problem you cannot expect viable answers.
Darin Dimitrov
Sure, providing a full code is always better. But, believe me, I`ve provided enough. The rest of the code will not give anything. If data can be received in the view (checked in debug) it`s sometihng in mvc display logic. The same code works in other view and I`ve provided the code. Other things I ignored seemed unrelated to me. Will try to investigate asap and post what was the problem here. Didnt mean to downvote your comment and appreciate your help. Will investigate and upvote it again.
Yaroslav Yakovlev
@Yaroslav, please see my update. I've provided you with a full example illustrating the concept. Still maintaining my point about editor templates though, and no, I don't believe you provided enough to reproduce the problem :-)
Darin Dimitrov