0

In my model, I have this :

 public class CustomerModel
    {
        public Customer Customer { get; set; }
        public Language Language { get; set; }

        public IList<Language> Languages { get; set; }

        public CustomerModel()
        {
            Language = new Language();
        }
    }

In my view, I have this :

<%: Html.DropDownList("Id", new SelectList(Model.Languages, "Id", "Code"))%>

that's work

But I'd like when I do a submit, have the Model.Language.Id setted to the value selected I tried this, but not work :

<%: Html.DropDownList(m => m.Language.Id, new SelectList(Model.Languages, "Id", "Code"))%>

Updat1: I used this solution, ok working

<%: Html.DropDownList("Language", 
new SelectList(ViewData.Model.Languages, "Id", "Code")) %>

When I select a customer, I'd like see the language change depending customer language, I did this :

<%: Html.DropDownList("Language", new SelectList(ViewData.Model.Languages, "Id", "Code", ViewData.Model.Customer.Language.Id)) %> 

nothing happen, I still see the first language of the list and not the customer language. The data in the controller are correct

Thanks,

2 Answers 2

1

use strongly typed html helper DropDownListFor

<%: Html.DropDownListFor(m => m.Language.Id, new SelectList(Model.Languages, "Id", "Code"))%>
Sign up to request clarification or add additional context in comments.

4 Comments

I added a comment see "Update1"
does Model.Languages contain Model.Customer.Language object?
Model.Languages has the languages possible (3 in my case) Model.Customer.Language is not null and has the value of one of the 3
then only thing i can imagine is that model.customer.language is being reset somewhere down the line. can u put controller code
0

This works fine for me (you need to use Html.DropDownListFor helper if you want to pass a lambda expression):

Model:

public class Language
{
    public int Id { get; set; }
    public string Code { get; set; }
}

public class CustomerModel
{
    public Language Language { get; set; }
    public IList<Language> Languages { get; set; }

    public CustomerModel()
    {
        Language = new Language();
        Languages = new List<Language>
        {
            new Language { Id = 1, Code = "en" },
            new Language { Id = 2, Code = "fr" },
        };
    }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new CustomerModel());
    }

    [HttpPost]
    public ActionResult Index(CustomerModel model)
    {
        return View(model);
    }
}

View:

<% using (Html.BeginForm()) { %>
    <%: Html.DropDownListFor(
        x => x.Language.Id, 
        new SelectList(Model.Languages, "Id", "Code")
    ) %>
    <input type="submit" value="OK" />
<% } %>

1 Comment

I added a comment see "Update1"

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.