繁体   English   中英

单一视图中来自不同模型的下拉框

[英]Dropdown boxes from different models in a single view

忍受我是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的下拉框,用户可以从中选择适当的值。 这时我很困惑。

`@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的下拉列表框。 lambda m不弹出任何模型属性。 有没有更好的方法可以做到这一点。 还是我现有的方法有任何问题。
任何帮助,将不胜感激。

在控制器操作方法中:

[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演示


编辑

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM