2
votes

I have a MVC dropdown list (Html.DropDownList) with list of Movies populated. I want to retrieve both Title(value field), Description(Text field) when I perform the form Submit. I can access the Title(value field), but I can't access the description. My code sample is below.

//View Model.......

public class Cinema

{

    public string CinemaName { get; set; }

    public SelectList MoviesList { get; set; }

    public string MoviesName { get; set; }

}

public class Movie

{

    public string Title { get; set; }

    public string Description { get; set; }

}

//Controller

[AcceptVerbs(HttpVerbs.Get)]

    public ActionResult Index()

    {

        Cinema _cinema = GetViewModel();

        ViewData.Model = _cinema;

        return View();

    }



    public IEnumerable<Movie> GetMovieList()

    {

        List<Movie> list = new List<Movie>();                        

        list.Add(new Movie(){ Title = "1", Description = "Batman" });

        list.Add(new Movie() { Title = "2", Description = "Metrix" });

        list.Add(new Movie() { Title = "3", Description = "Jaws" });

        return list;           

    }



    public Cinema GetViewModel()

    {

        var cinema = new Cinema();

        cinema.CinemaName = "Village";

        cinema.MoviesList = new SelectList(GetMovieList(), "Title", "Description", "Jaws");

        return cinema;

    }



    [AcceptVerbs(HttpVerbs.Post)]

    public ActionResult Update(Cinema _cinema)

    {

        //Here I need both value and the text  field from the selected item in the drop down

        string movieName = _cinema.MoviesName;

        return View();

  }

//View

" %>

Home Page

<% using (Html.BeginForm("Update", "Home"))

   { %>

<p>

    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">

        http://asp.net/mvc</a>.

</p>

<%= Html.TextBox("CinemaName", Model.CinemaName)%>

<%= Html.DropDownList("MoviesName", Model.MoviesList)%>



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

<% } Html.EndForm(); %>

} {

8

8 Answers

5
votes

A form post only sends the value of a select list. You shouldn't need anything else. You should be able to get what you want from the value.

2
votes

Start by considering why you need the text too...

technically the dropdown Id should be enough to identify an option in the controller side : )

2
votes

Per one of your answers in this thread...

I need the text because to display the description in the next View. Also I don't need to make a separate call to db to get the text field by providing Id. And I do not want to store the list in memory to get the text field either. Is there a Javascript or jQuery solution for this?

It seems to me that the controller for this "next view" you mention should be able to create and return the updated/new View Model to the "next view." I personally find this to be easier to review and modify with changes going forward.

You could add a hidden field or text box that has it's text property changed anytime your dropdown changes. This new field would then be posted back to your controllers as well.


0
votes

I need the text because to display the description in the next View. Also I don't need to make a separate call to db to get the text field by providing Id. And I do not want to store the list in memory to get the text field either. Is there a Javascript or jQuery solution for this?

0
votes

I thought something like this is much better. One go, get the list the way I need and manipulate the FullName on post.

public class Movie

 {  

        public string Title { get; set; }  

        public string Description { get; set; } 
        public string FullName{ get{ return Title + "|" + Description; }} // instead of | you can put esc char or other char that does not easily find in title + description

 }  

cinema.MoviesList = new SelectList(GetMovieList(), "Title", "FullName", "Jaws");

0
votes

Of course you can do something like:

$("#yourdropdownid option:selected").text();

Original thread : )

0
votes

As Mattias Jakobson said you can only access the Value attribute of the selected when you send the form. Should you really need both the label and the value, a quick and dirty approach would be to have both values separated by a : in the Value attribute, something like and then do a string.split.

Having given an answer to your question I suggest you reconsider your approach and try to get the whole domain object out the selected option inside your controller (passing an id would be the ideal solution).

0
votes

Yep agree. I think I slightly posted the wrong code. I guess the correct code is

cinema.MoviesList = new SelectList(GetMovieList(), "FullName", "Description", "Jaws");

class Movie
{  

        public string Title { get; set; }  

        public string Description { get; set; } 
        public string FullName{ get{ return Title + "|" + Description; }} // instead of | you can put esc char or other char that does not easily find in title + description

 }

Yo see here the original Title is now act as the FullName (Title + "|" + Description) and Description is what we display in the View.

<%= Html.DropDownList("Description", Model.MoviesList)%>

So the controller doesn't know how the View looks like. Just the ID is manipulated in the controller for the ease of getting the Description during during the post. Let me know if I'm still wrong?

What I explained is just another option. You are correct I think the best possible solution is to call the db with ID to to get the Description on postback