我以表格格式显示数据。该表是在使用时自动生成的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; }
}
主视图索引.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>
指导我如何将我的性别数据从主索引视图传递到性别视图编辑器模板 folder.
Edit
在主视图中我添加这一行
@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 中,我更改了行 like
<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>
但仍然没有运气。由于缺乏对 ASP.NET MVC 的控制而严重卡住编辑器模板现在单选按钮即将推出,但默认情况下会选择所有单选按钮,这是错误的。查看最新的用户界面。
请帮助我摆脱这个问题。谢谢