Altaf Khatri
Altaf Khatri



RSS feed for http://www.AltafKhatri.com website. Twitter account of http://www.AltafKhatri.com website.
∗Recently Added - How to take care of Betta fighter fish and increase life span?  10/30/2013


Find me on web

 LinkedIn

 Facebook

 Twitter

ASP.NET MVC Postback - dropdownlist example

In ASP.NET MVC, postback is handled differently then how it is handled in ASP.NET.

If you are starter then you will be searching for ways to do this. In this article I will discuss more about handling dropdownlist in the postback of the ASP.NET MVC view page.

In my previous article I had discussed How to bind IList with MVC dropdownlist. I will use that example in this article. I am using the same master page that comes with standard ASP.NET MVC project in Visual Studio.

Controller Name: PostBackCheck
Action Name: Index

The view page looks like this:

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

 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

      Index

</asp:Content>

 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

 

    <h2>Index</h2>

    <form id="frm_Main1" action="Index" method="post">

        <div>

        <%=Html.DropDownList("SecretQuestion") %><p/>

        <input type="submit" value="Do PostBack" id="submit" name="btnSubmit"/>

       

        </div>

    </form>

 

</asp:Content>

To populate the dropdownlist with items I have used a method: GetDropdownlistItems()

Method 1: Bind dropdowlist value using FormCollection object:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;

 

namespace MvcApplication1.Controllers

{

    public class PostBackCheckController : Controller

    {

        //

        // GET: /PostBackCheck/

 

        public ActionResult Index()

        {

            IList<SelectListItem> ilSelectListItems = GetDropdownlistItems ();

            ViewData["SecretQuestion"] = ilSelectListItems;

            return View();

        }

 

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Index(FormCollection frmCollection)

        {

            string dropDownSelectedValue = frmCollection.Get("SecretQuestion");

 

            IList<SelectListItem> ilSelectListItems = GetDropdownlistItems ();

 

            ViewData["SecretQuestion"] = new SelectList (ilSelectListItems, "Value", "Text", dropDownSelectedValue);

            return View();

        }

 

        public IList<SelectListItem> GetDropdownlistItems()

        {

            //1. Create a new IList of SelectListItems

            //2. Add 10 items to the list.

            IList<SelectListItem> ilSelectList = new List<SelectListItem>();

            for (int cnt = 0; cnt < 10; cnt++)

            {

                SelectListItem selectListItem = new SelectListItem();

                selectListItem.Text = "Altaf" + cnt.ToString();

                selectListItem.Value = cnt.ToString();

                ilSelectList.Add(selectListItem);

            }

            return ilSelectList;

        }

    }

}

FormCollection object will have all the objects on the form. We can get the dropdownlist selected value using: frmCollection.Get("SecretQuestion").

Method 2: Bind dropdownlist value using the object name.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;

 

namespace MvcApplication1.Controllers

{

    public class PostBackCheckController : Controller

    {

        //

        // GET: /PostBackCheck/

 

        public ActionResult Index()

        {

            IList<SelectListItem> ilSelectListItems = GetDropdownlistItems ();

            ViewData["SecretQuestion"] = ilSelectListItems;

            return View();

        }

 

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Index(string SecretQuestion)

        {

            IList<SelectListItem> ilSelectListItems = GetDropdownlistItems ();

            ViewData["SecretQuestion"] = new SelectList (ilSelectListItems, "Value", "Text", SecretQuestion);

            ViewData["SecretQuestion"] = ilSelectListItems;

            return View();

        }

 

        public IList<SelectListItem> GetDropdownlistItems()

        {

            //1. Create a new IList of SelectListItems

            //2. Add 10 items to the list.

            IList<SelectListItem> ilSelectList = new List<SelectListItem>();

            for (int cnt = 0; cnt < 10; cnt++)

            {

                SelectListItem selectListItem = new SelectListItem();

                selectListItem.Text = "Altaf" + cnt.ToString();

                selectListItem.Value = cnt.ToString();

                ilSelectList.Add(selectListItem);

            }

            return ilSelectList;

        }

    }

}


Here we can get the dropdownlist selected value by accepting it as part of the method signature.
The input parameter name should be the same as the object name.

Hope this helps.





SiteMap  |  Contact Me