使用EditorTemplates和单选按钮

问题描述 投票:2回答:1

我以表格格式显示数据。使用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>

但仍然没有运气。由于缺乏对asp.net mvc控制器的严重困难,现在单选按钮即将到来,但默认情况下都会选中所有错误。看到最新的UI。

enter image description here

请帮我解决这个问题。谢谢

c# asp.net-mvc
1个回答
1
投票

你的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>
© www.soinside.com 2019 - 2024. All rights reserved.