我用表格的格式显示数据。在使用EditorFor和EditorTemplates时,将自动生成表。
在表格的每一行中,我都显示ID、姓名、国家下拉列表、业余爱好选择复选框和性别选择单选按钮。
所有的工作都很好,但我不能为性绑定单选按钮。我无法理解我错过了什么,因为什么我得到了错误。
请看我的代码,并告诉我的方向,以改变什么为单选按钮。
我的完整代码
控制器代码
public class HomeController : Controller
{
public ActionResult Index()
{
StudentListViewModel osvm = new StudentListViewModel();
osvm.Sex = osvm.GetSex();
osvm.Countries = osvm.GetCountries();
return View(osvm);
}
[HttpPost]
public ActionResult Index(StudentListViewModel oStudentListViewModel)
{
return View(oStudentListViewModel);
}
}视图模型
public class StudentListViewModel
{
//public List<Country> Country { get; set; }
public List<SelectListItem> Countries { get; set; }
public IList<Student> Students { get; set; }
public List<Sex> Sex { get; set; }
public StudentListViewModel()
{
Students = new List<Student>
{
new Student
{
ID=1,Name="Keith",CountryID="0",SexID="F",
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=true},
new Hobby{ID=2,Name="Hocky",Checked=false},
new Hobby{ID=3,Name="Cricket",Checked=false}
}
},
new Student
{
ID=2,Name="Paul",CountryID="2",
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=false},
new Hobby{ID=2,Name="Hocky",Checked=true},
new Hobby{ID=3,Name="Cricket",Checked=false}
}
},
new Student
{
ID=3,Name="Sam",CountryID="3",
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=false},
new Hobby{ID=2,Name="Hocky",Checked=false},
new Hobby{ID=3,Name="Cricket",Checked=true}
}
}
};
}
public List<Sex> GetSex()
{
Sex = new List<Sex>
{
new Sex{ID="M",SexName="Male"},
new Sex{ID="F",SexName="Female"}
};
return Sex;
}
public List<SelectListItem> GetCountries()
{
Countries = new List<SelectListItem>
{
new SelectListItem{Value="1",Text="India"},
new SelectListItem{Value="2",Text="UK"},
new SelectListItem{Value="3",Text="USA"}
};
return Countries;
}
}模型类
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string CountryID { get; set; }
public string SexID { get; set; }
public IList<Hobby> Hobbies { get; set; }
}
public class Hobby
{
public int ID { get; set; }
public string Name { get; set; }
public bool Checked { get; set; }
}
public class Sex
{
public string ID { get; set; }
public string SexName { get; set; }
}主视图Index.cshtml
@model EditorTemplateSample.Models.StudentListViewModel
@{
ViewBag.Title = "Home Page";
}
<br /><br />
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div class="form-group">
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-hover">
<tr>
<th>
ID
</th>
<th>
Name
</th>
<th>
Country
</th>
<th>
Hobbies
</th>
<th>
Sex
</th>
</tr>
<tbody>
@Html.EditorFor(m => m.Students, new { Countries = Model.Countries, Sex = Model.Sex })
</tbody>
</table>
</div>
</div>
}EditorTemplates\Student.cshtml
@model EditorTemplateSample.Models.Student
<tr>
<td>
@Html.HiddenFor(m => m.ID)
@Html.DisplayFor(m => m.ID)
</td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
<td>
@Html.DropDownListFor(m => m.CountryID,
new SelectList((List<SelectListItem>)ViewData["Countries"], "Value", "Text", Model.CountryID), "-- Select Country--")
<td>
<td>
@Html.EditorFor(m => m.Hobbies)
<td>
<td>
@Html.EditorFor(m => ((EditorTemplateSample.Models.Sex) ViewData["Sex"]).ID)
<td>
</tr>EditorTemplates\Hobby.cshtml
@model EditorTemplateSample.Models.Hobby
<div class="checkbox">
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.Name)
@Html.CheckBoxFor(m => m.Checked)
@Html.LabelFor(m => m.Checked, Model.Name)
</div>EditorTemplates\Sex.cshtml
@model EditorTemplateSample.Models.Sex
<td>
<div class="checkbox">
@Html.HiddenFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID)
@Html.HiddenFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).SexName)
@Html.RadioButtonFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID, ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID)
@Html.LabelFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID, ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).SexName)
</div>
</td>@Html.EditorFor(m => m.Students, new { Countries = Model.Countries, Sex = Model.Sex })以上述方式将性模型数据传递给Student.cshtml文件
从Student.cshtml文件中,我尝试绑定ID @Html.EditorFor(m => ((EditorTemplateSample.Models.Sex) ViewData["Sex"]).ID)
中的EditorTemplates\sex.cshtml文件
@model EditorTemplateSample.Models.Sex
<td>
<div class="checkbox">
@Html.HiddenFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID)
@Html.HiddenFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).SexName)
@Html.RadioButtonFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID, ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID)
@Html.LabelFor(m => ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).ID, ((EditorTemplateSample.Models.Sex)ViewData["Sex"]).SexName)
</div>
</td>指导我如何将我的性数据从主要索引视图传递到EditorTemplates文件夹中的性视图。
编辑
在主视图中,我添加了这一行
@Html.EditorFor(m => m.Students, new { Countries = Model.Countries, MainModel = Model, Sex = Model.Sex })在student.cshtml中,我像@Html.EditorFor(m => ((EditorTemplateSample.Models.StudentListViewModel)ViewData["MainModel"]).Sex, new { Sex = (List<EditorTemplateSample.Models.Sex>)ViewData["Sex"] })一样编辑行
在sex.cshtml中,为单选按钮生成,我更改了行喜欢
<div class="checkbox">
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.SexName)
@Html.RadioButtonFor(m => m.ID,Model.ID)
@Html.LabelFor(m => m.ID, Model.SexName)
</div>但还是没有运气。由于缺乏对mvc EditorTemplates的控制,现在单选按钮出现了,但是所有的按钮都是默认选择的,这是错误的。请参阅最新的UI。

请帮我解决这个问题。谢谢
发布于 2018-02-26 22:12:43
您的Student类包含一个属性string SexID,您希望将所选的单选按钮值绑定到该属性。但是您的EditorTemplate用于一个类型为Sex的模型,而您的Student模型不包含一个Sex类型的属性(也不应该包含该属性)。
在这种情况下,使用EditorTemplate是没有意义的--绑定到一个简单的属性,而不是一个复杂的对象或对象集合。单选按钮应在Student.cshtml模板中生成。
@model EditorTemplateSample.Models.Student
<tr>
<td>
@Html.HiddenFor(m => m.ID)
@Html.DisplayFor(m => m.ID)
</td>
<td>@Html.TextBoxFor(m => m.Name)</td>
<td>@Html.DropDownListFor(m => m.CountryID, new SelectList((List<SelectListItem>)ViewData["Countries"], "Value", "Text", Model.CountryID), "-- Select Country--")</td>
<td>@Html.EditorFor(m => m.Hobbies)</td>
<td>
@foreach(var sex in (List<Sex>)ViewData["Sex"])
{
<label>
@Html.RadioButtonFor(m => m.SexID, sex.ID, new { id = "" })
<span>@sex.SexName</span>
</label>
}
</td>
</tr>https://stackoverflow.com/questions/48989710
复制相似问题