单个视图中来自不同型号的下拉框

时间:2016-12-23 05:26:41

标签: c# asp.net asp.net-mvc

请耐心等待,我是Asp.net MVC的新手。

我有以下型号

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;}

}

在我的视图中我通过了StorePerson模型。我想显示一个视图拥有人物模型的所有属性,即id,name以及District,Tehsil,UnionCouncil的Dropdownboxes,用户可以从中选择适当的值。此刻我很难过。

`@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)

如何在视图中获取District,UnionCouncil,Tehsil的Dropdownbox。 lambda m不会弹出任何模型属性。有没有更好的方法来做到这一点。或者我现有的方法有什么问题 任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

  

在您的控制器操作方法中:

[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);
}
  

在您看来:

<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>

这是 DotNetFiddle Demo

  

编辑

[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);
}
  

在您看来:

@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" />
    }