Hello,
i am trying to create two dropdownlist one is having list of designations and another is having a list of names on the basis of designations
i am using only one database from this so i am not able to get the distinct values in dropdownlist 1 with designation name and on the basis of that usernames in other dropdownlist.
please help
@model WebApplication2.Models.CascadingModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Register", FormMethod.Post))
{
<div class="form-horizontal">
<div class="panel panel-primary" style="width:620px;margin-left:auto;margin-right:auto;margin-top:30px">
<div class="panel-heading">
<h4>User Registration</h4>
</div>
<div class="panel-body">
<div class="form-group">
@Html.Label("Select Designation", htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.DropDownListFor(model=>model.Designationname, new SelectList(ViewBag.desingations, "Designation","Designation"), htmlAttributes: new { onchange = "documents.fors[0].submit();", @class = "form-control" })
</div>
</div>
</div>
</div>
</div>
}
using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class RegisterController : Controller
{
// GET: Register
public ActionResult Index()
{
ApplicationDbContext db = new ApplicationDbContext();
CascadingModel model = new CascadingModel();
//var query = model.Designation.DistinctBy(x => new { x.Text, x.Value });
List<SelectListItem> desingations = new List<SelectListItem>();
desingations = (from c in db.Deisngations
select new SelectListItem { Text = c.Designation, Value = c.DesigId.ToString() }).Distinct().ToList();
//var uniquerdesig = desingations.Distinct();
ViewBag.designation = desingations;
return View();
}
}
}
namespace WebApplication2.Models
{
public class CascadingModel
{
public CascadingModel()
{
this.Designation = new List<SelectListItem>();
this.Username = new List<SelectListItem>();
this.Email = new List<SelectListItem>();
}
public List<SelectListItem> Designation { get; set; }
public List<SelectListItem> Username { get; set; }
public List<SelectListItem> Email { get; set; }
public string Designationname { get; set; }
public string user { get; set; }
public string EmilId { get; set; }
}
}