Reputation: 45
Bear with me i am new to Asp.net MVC.
I have the following Models
public class District {
public int id{get;set;}
public String districtName{get;set;}
public virtual ICollection<Tehsil> tehsil{get;set;}
}
public class Tehsil{
public int id{get;set;}
public String tehsilName{get;set;}
public virtual ICollection<UnionCouncil> uc{get;set;}
}
public class UnionCouncil {
public int id{get;set;}
public String ucName{get;set;}
}
public class Person{
public int id{get;set;}
public int name{get;set;}
public int districtId{get;set;}
public int tehsilId{get;set;}
public int UnionCouncilId{get;set;}
}
public class StorePerson{
public Person person{get;set;}
public District district{get;set;}
public Tehsil tehsil {get;set;}
public UnionCouncil unionCouncil {get;set;}
}
In my View i Pass the StorePerson Model. I want to Display a view Having all the attributes of the person model i.e id, name along with Dropdownboxes of District, Tehsil, UnionCouncil from which the User can select the appropriate value. I am stumped here at this point.
`@model DistrictExample.Models.StorePerson
@{
ViewBag.Title = "CreatePerson";
}
<h2>CreatePerson</h2>
@Html.TextBoxFor(m=>m.person.name)
@Html.DropDownListFor(Model.person.districtId,new SelectList(m=>m)
How do i get the Dropdownbox for District, UnionCouncil, Tehsil in the view. The lambda m doesn't popout any model property. Is there a better way to do it. Or is there any problem with my existing approach.
Any help would be appreciated.
Upvotes: 0
Views: 54
Reputation: 3520
In your controller action method:
[HttpGet]
public ActionResult Index()
{
//All this is dummy data, you can bind Lists to Database data too.
List<District> lstDistricts = new List<District>();
lstDistricts.Add(new District() { id=1, districtName="test01" });
lstDistricts.Add(new District() { id = 2, districtName = "test02" });
List<Tehsil> lstTehsil = new List<Tehsil>();
lstTehsil.Add(new Tehsil() { id = 1, tehsilName = "test01" });
lstTehsil.Add(new Tehsil() { id = 2, tehsilName = "test02" });
List<UnionCouncil> lstUnionCouncil = new List<UnionCouncil>();
lstUnionCouncil.Add(new UnionCouncil() { id = 1, ucName = "test01" });
lstUnionCouncil.Add(new UnionCouncil() { id = 2, ucName = "test02" });
StorePerson sp = new StorePerson();
Person p = new Person();
p.name = "Zulu Khan";
p.id = 1;
p.districtId = 2;
p.UnionCouncilId = 1;
p.tehsilId = 2;
sp.person = p;
sp.Districts = lstDistricts;
sp.Tehsils = lstTehsil;
sp.UnionCouncils = lstUnionCouncil;
return View(sp);
}
In your view:
<div class="col-md-6">
@Html.LabelFor(x => x.person.name)
@Html.DisplayFor(x => x.person.name)
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.id)
@Html.DisplayFor(x => x.person.id)
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.districtId)
@Html.DropDownListFor(x => x.person.districtId, new SelectList(Model.Districts, "id", "districtName"))
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.tehsilId)
@Html.DropDownListFor(x => x.person.tehsilId, new SelectList(Model.Tehsils, "id", "tehsilName"))
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.UnionCouncilId)
@Html.DropDownListFor(x => x.person.UnionCouncilId, new SelectList(Model.UnionCouncils, "id", "ucName"))
</div>
Here is its DotNetFiddle Demo
Edit
[HttpGet]
public ActionResult Index()
{
//All this is dummy data, you can bind Lists to Database data too.
List<District> lstDistricts = new List<District>();
lstDistricts.Add(new District() { id=1, districtName="test01" });
lstDistricts.Add(new District() { id = 2, districtName = "test02" });
List<Tehsil> lstTehsil = new List<Tehsil>();
lstTehsil.Add(new Tehsil() { id = 1, tehsilName = "test01" });
lstTehsil.Add(new Tehsil() { id = 2, tehsilName = "test02" });
List<UnionCouncil> lstUnionCouncil = new List<UnionCouncil>();
lstUnionCouncil.Add(new UnionCouncil() { id = 1, ucName = "test01" });
lstUnionCouncil.Add(new UnionCouncil() { id = 2, ucName = "test02" });
StorePerson sp = new StorePerson();
sp.Districts = lstDistricts;
sp.Tehsils = lstTehsil;
sp.UnionCouncils = lstUnionCouncil;
return View(sp);
}
In your view:
@using (Html.BeginForm("SubmitActionName","Home", FormMethod.Post))
{
<div class="col-md-6">
@Html.LabelFor(x => x.person.name)
@Html.TextBoxFor(x => x.person.name)
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.id)
@Html.TextBoxFor(x => x.person.id)
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.districtId)
@Html.DropDownListFor(x => x.person.districtId, new SelectList(Model.Districts, "id", "districtName"))
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.tehsilId)
@Html.DropDownListFor(x => x.person.tehsilId, new SelectList(Model.Tehsils, "id", "tehsilName"))
</div>
<div class="col-md-6">
@Html.LabelFor(x => x.person.UnionCouncilId)
@Html.DropDownListFor(x => x.person.UnionCouncilId, new SelectList(Model.UnionCouncils, "id", "ucName"))
</div>
<input type="submit" value="Submit" />
}
Upvotes: 1