I see that you are using forms with the web controls runat="server" and asp:XXX . These concepts should never be used in ASP.NET MVC. There are no more ViewState and PostBacks these server controls rely on.
So, in ASP.NET MVC, you should start by defining a view model that represents the data:
public class ItemsViewModel { public string SelectedItemId { get; set; } public IEnumerable<SelectListItem> Items { get; set; } }
then you must define a controller with two actions (one of which displays the view, and the other with the processing of the form):
public class HomeController : Controller { public ActionResult Index() { var model = new ItemsViewModel { Items = new[] { new SelectListItem { Value = "Theory", Text = "Theory" }, new SelectListItem { Value = "Appliance", Text = "Appliance" }, new SelectListItem { Value = "Lab", Text = "Lab" } } }; return View(model); } [HttpPost] public ActionResult Index(ItemsViewModel model) {
and finally, you should write the corresponding strictly typed Index view:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<AppName.Models.ItemsViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Home Page </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) { %> <%= Html.DropDownListFor(x => x.SelectedItemId, new SelectList(Model.Items, "Value", "Text")) %> <input type="submit" value="OK" /> <% } %> </asp:Content>
Having said that, you can also program this choice inside your view (although I would not recommend this):
<% using (Html.BeginForm()) { %> <select name="selectedItem"> <option value="Theory">Theory</option> <option value="Appliance">Appliance</option> <option value="Lab">Lab</option> </select> <input type="submit" value="OK" /> <% } %>
and have the following controller:
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string selectedItem) {
Darin Dimitrov
source share