I have found this on internet from this site
http://www.altafkhatri.com/Technical/ASP-NET-MVC-Dropdownlist/How-to-post-back/on-selectedIndexChanged
It is really helpful post. I put this on here future reference.
Steps to create the postback in ASP.Net MVC dropdownlist selectedindex change event:
Create a controller - Dropdownlist. Paste the code in the section below.
Create a view of the GetDD action binded to the strongly typed object(CoverObject). Paste the code in the section below.
Code in the controller
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 CoverObject
{
public IList
public string SelectedName { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string formAction { get; set; }
public CoverObject()
{
iliSLI = new List
SelectListItem sli = new SelectListItem();
sli.Text = "Altaf";
sli.Value = "1";
iliSLI.Add(sli);
sli = new SelectListItem();
sli.Text = "Krish";
sli.Value = "22";
iliSLI.Add(sli);
sli = new SelectListItem();
sli.Text = "Sameer";
sli.Value = "3333";
iliSLI.Add(sli);
sli = new SelectListItem();
sli.Text = "Iqbal";
sli.Value = "44444";
iliSLI.Add(sli);
sli = new SelectListItem();
sli.Text = "Maimoona";
sli.Value = "5555";
iliSLI.Add(sli);
}
}
public class DropdownlistController : Controller
{
//
// GET: /Dropdownlist/
public ActionResult Index()
{
return View();
}
public ActionResult GetDD()
{
CoverObject co = new CoverObject();
co.formAction = "";
//ViewData["SecretQuestion"] = co.iliSLI;
return View(co);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetDD(CoverObject co)
{
CoverObject coNew = new CoverObject();
string indexChangedValue = co.SelectedName;
var item = from it in coNew.iliSLI
where it.Value == co.SelectedName
select it;
item.First().Selected = true;
coNew.Name = item.First().Text;
coNew.Address = co.Address;
if (!string.IsNullOrEmpty(co.formAction) && co.formAction.Equals("Submit Form By Clicking"))
{
// Based on the main submit of the form, it can process and redirect or stay on the same view.
ModelState.AddModelError("_FORM", "Form Submitted by clicking submit button");
}
else//Submitted by dropdownlist index change event
{
//DROPDOWNLIST on selectedIndexChangedEvent Handled hereModelState.AddModelError("_FORM", "Form Submitted by clicking submit button");
ModelState.AddModelError("iliSLI", "Form Submitted by selected Index Change event");
}
return View(coNew);
}
}
}
Code in the aspx file
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
<%= Html.ValidationSummary("Form submitted by what event notification:") %>
<% using (Html.BeginForm()) {%>
<% } %>
No comments:
Post a Comment