views:

212

answers:

8

Hi

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

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

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&lt;/a&gt;.

</p>

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

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



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

<% } Html.EndForm(); %>

} {

+4  A: 

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.

Mattias Jakobsson
+2  A: 

Hi Raji,

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

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

SDReyes
A: 

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?

Rajithakba
You can post it in a hidden field. But you relay should get it from the db. What you want is bad practice, it will hurt you later on and performance vice its probably better to get it from the db anyway. If you post it you will have more network traffic (although very little in this case) and that will usually be a bigger hit on performance then a small database call. And if you get it from the db you should probably hit a cache anyway. My advice: send only the id and get the rest form the db.
Mattias Jakobsson
A: 

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");

Rajithakba
Well, this way your controller needs to know how the view looks like (it shouldn't. That violates the pattern). You relay are a lot better of only posting the id. Any reason why you don't want that?
Mattias Jakobsson
Hi MattiasMy answer is submitted below. "Yep agree. I think I slightly posted the wrong code.........."Please let me know what you think about it.
rajithakba
A: 

Hi again Raj...

Of course you can do something like:

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

Original thread : )

SDReyes
+2  A: 

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.


If it were up to me, my controller would populate my ViewModel with the information it needs for the new view. Using hidden fields as you've requested information on feels very much like a code smell to me.

RSolberg
A: 

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

rajithakba
A: 

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).

JoseMarmolejos