使用EditorTemplates和单选按钮 [英] Working with EditorTemplates and radio buttons

查看:81
本文介绍了使用EditorTemplates和单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以表格格式显示数据.在使用 EditorFor和EditorTemplates 时,表格是自动生成的.

I am showing data in tabular format. the table is generated automatically when working with EditorFor and EditorTemplates.

在表格的每一行中,我都显示ID,姓名,国家/地区下拉列表,用于选择爱好的复选框以及用于选择性别的单选按钮.

in each row of table i am showing ID, Name, Country dropdown, checkboxes for hobbies selection and radio button for sex selection.

一切正常,但我无法绑定用于性爱的单选按钮. 我无法理解我所缺少的错误.

all are working fine but i am not being able to bind radio buttons for sex. i am not being able to understand what i am missing for which i am getting error.

请查看我的代码,并向我指明单选按钮的更改内容.

please have a look at my code and give me direction what to change for radio buttons.

控制器代码

    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);
        }
}

viewmodel

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文件

@Html.EditorFor(m => m.Students, new { Countries = Model.Countries, Sex = Model.Sex }) the above way i pass Sex model data to Student.cshtml file

从Student.cshtml文件中,我尝试绑定ID @Html.EditorFor(m => ((EditorTemplateSample.Models.Sex) ViewData["Sex"]).ID)

from Student.cshtml file i try to bind 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 文件夹中的性别视图.

guide me how could i pass my sex data from main index view to sex view in EditorTemplates folder.

在主视图中,我添加此行

in main view i add this line

@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"] })

in student.cshtml i edit line like @Html.EditorFor(m => ((EditorTemplateSample.Models.StudentListViewModel)ViewData["MainModel"]).Sex, new { Sex = (List<EditorTemplateSample.Models.Sex>)ViewData["Sex"] })

在sex.cshtml中用于生成单选按钮,我更改了行喜欢

in sex.cshtml for radio button generation i changed line likes

<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 EditorTemplates 的控制不足,严重卡住了,现在单选按钮出现了,但是默认情况下所有这些都处于选中状态,这是错误的.查看最新的用户界面.

but still no luck. badly stuck due to lack of control over asp.net mvc EditorTemplates now radio buttons are coming but all are selected by default which is wrong. see the latest UI.

请帮助我摆脱这个问题.谢谢

please help me to get out of this problem. thanks

推荐答案

您的Student类包含属性string SexID,这是您要将所选单选按钮值绑定到的属性.但是您的EditorTemplate用于typeof Sex的模型,并且您的Student模型不包含typeof Sex的属性(也不应该).

Your Student class contains a property string SexID which is what you are wanting to bind the selected radio button value to. But your EditorTemplate is for a model that is typeof Sex, and you Student model does not contain a property which is typeof Sex (and nor should it).

在这种情况下使用EditorTemplate毫无意义-您绑定到简单属性,而不是复杂对象或对象集合.单选按钮应该在您的Student.cshtml模板中生成.

Using an EditorTemplate in this case makes no sense - your binding to a simple property, not a complex object or collection of objects. The radio buttons should be generated in your Student.cshtml template.

@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>

这篇关于使用EditorTemplates和单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆