<div class="form-group">
@Html.LabelFor(model => Model.countyid, new {@class = "control-label"})
@Html.EditorFor(model => Model.countyid, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.countyid)
</div>
我的主表有一个countyid的外键,它链接到县表。上面是我的编辑。
问题-我需要将id转换为另一个表中的名称列表,这样用户就不会看到这些id号。如果我能在这个理论上得到一些帮助,也许还有一个类似问题的例子,我将不胜感激。感谢
编辑:说明-我的第一个表有其他数据,但外键是郡id。郡表有id和名称,我需要将名称拉成与我在第一个表中使用的相同的形式,替换id,然后从所选名称发回id。
型号:郡
public partial class County
{
public int id { get; set; }
public int countryid { get; set; }
public bool deleted { get; set; }
public string name { get; set; }
}
公司:
public partial class Agency
{
public int id { get; set; }
public bool deleted { get; set; }
public string name { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string address3 { get; set; }
public string town { get; set; }
public int countyid { get; set; }
public string postcode { get; set; }
public string telephoneno { get; set; }
public string companyno { get; set; }
}
您的模型中应该包含的不仅仅是id:
public class CountyInfo{
public int countyid {get;set;}
public string CountyName{get;set;}
//Whatever other properties you want in the model to use on the page
}
既然你说了一个id列表,我假设你想像这样一次从数据库模型中提取多个记录,并将你从表中得到的内容映射到它:
public class CountyInfoList{
public List<CountyInfo> Counties {get;set;}
//other props that go to counties in general
}
然后你可以把它添加到你的主模型中
public class PrimaryTableModel {
public int PrimaryTableID {get;set;}
//if there's a one to one relationship between primary table and county
public CountyInfo County {get;set;}
//or if there's a many to one relationship
public CountyInfoList CountiesList {get;set;}
}
我不认为你打算在编辑器中使用Id字段。如果这是您正在编辑的对象,请将id隐藏在上面。应该是这样的:
@Html.HiddenFor(model=>model.countyid)
@Html.EditorFor(model => Model.CountyName, new {htmlAttributes = new {@class = "form-control"}})
或者如果使用主表模型
@Html.HiddenFor(model=>model.County.countyid)
@Html.EditorFor(model => Model.County.CountyName, new {htmlAttributes = new {@class = "form-control"}})
@foreach (var c in Model.CountiesList.Counties )
{
@Html.HiddenFor(model=>c.countyid)
@Html.EditorFor(model => c.CountyName, new {htmlAttributes = new {@class = "form-control"}})