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</a>.
</p>
<%= Html.TextBox("CinemaName", Model.CinemaName)%>
<%= Html.DropDownList("MoviesName", Model.MoviesList)%>
<input type="submit" value="Submit" />
<% } Html.EndForm(); %>
} {