使用 MVC Razor 传递列表对象 [英] Pass list objects with MVC Razor

查看:36
本文介绍了使用 MVC Razor 传递列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个对象,但在对象内部我有一个属性,它是一个对象列表.我想传递带有列表对象的对象,但是当我提交表单时列表是空的.我不知道如何处理.

课程

公开课VSTtendance{公共 int MemberID { 获取;放;}公共字符串名称 { 获取;放;}公共字符串姓氏 { 获取;放;}public bool 服务员{ get;放;}}公开课服务出勤{公共 int 出席 ID { 得到;放;}公共 int 出席类型 ID { 获取;放;}公共字符串出勤类型 { 获取;放;}公共日期时间服务 { 获取;放;}公共 int ServiceID { 获取;放;}公共字符串扬声器{获取;放;}公共字符串位置{获取;放;}公共 int HeadCount { 得到;放;}公共 int 类别 ID { 获取;放;}公共字符串 描述 { 获取;放;}公共字符串 ChurchNotes { 获取;放;}公共字符串 ServingNotes { 获取;放;}公共日期时间日期创建{获取;放;}公共日期时间 CreatedBy { 获取;放;}公共日期时间日期更新{获取;放;}公共日期时间更新通过 { 获取;放;}公共列表<VSTATtendance>会员出勤{得到;放;}}

HTML

@using (Html.BeginForm("Attendance", "Home", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl, @class = "form-horizo​​ntal", role = "form" })){<div class="form-group"><label for="inputSpeaker" class="col-sm-2 control-label">扬声器<div class="col-sm-6">@Html.TextBoxFor(m => m.Speaker, new { @class = "form-control", placeholder = "Speaker", type = "text", required = "required " })

<table class="table table-bordered table-condensed table-hover"><tr class="警告"><td></td><td>姓名</td><td>姓氏</td></tr>@foreach(Model.MemberAttendance 中的变量项目){<tr><td>@Html.CheckBox("Attendant")</td><td>@item.Name</td><td>@item.Surname</td></tr>}<div class="form-group"><div class="col-sm-10"><button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-ok"></span>提交</button>

}

解决方案

您必须使用 for 而不是 foreach 并使用 Hidden for .我已经测试了以下内容并且完美

 @model ServiceAttendance@{ViewBag.Title = "出勤";}<h2>出勤</h2>@using (Html.BeginForm()){<div class="form-group"><label for="inputSpeaker" class="col-sm-2 control-label">扬声器<div class="col-sm-6">@Html.TextBoxFor(modelItem => modelItem.Speaker)

<table class="table table-bordered table-condensed table-hover"><tr class="警告"><td></td><td>姓名</td><td>姓氏</td></tr>@for (int i=0;i Model.MemberAttendance[i].Attendant)</td><td>@Model.MemberAttendance[i].Name@Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td><td>@Model.MemberAttendance[i].Surname@Html.HiddenFor(it=>Model.MemberAttendance[i].Surname)</td></tr>}<div class="form-group"><div class="col-sm-10"><button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-ok"></span>提交

}

所以你要做的就是用下面的替换 foreach

 @for (int i=0;i Model.MemberAttendance[i].Attendant)</td><td>@Model.MemberAttendance[i].Name@Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td><td>@Model.MemberAttendance[i].Surname@Html.HiddenFor(it=>Model.MemberAttendance[i].Surname)</td></tr>}

I am using an object but inside object I have a property that is a list of objects. I want to pass that object with the list objects inside it but the list is empty when I submit my form. I am not sure how to handle this.

Class

public class VSTAttendance
{
    public int MemberID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public bool Attendant { get; set; }
}

public class ServiceAttendance 
{
    public int AttendanceID { get; set; }
    public int AttendanceTypeID { get; set; }
    public string AttendanceType { get; set; }
    public DateTime Service { get; set; }
    public int ServiceID { get; set; }
    public string Speaker { get; set; }
    public string Location { get; set; }
    public int HeadCount { get; set; }
    public int CategoryID { get; set; }
    public string Description { get; set; }
    public string ChurchNotes { get; set; }
    public string ServingNotes { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime CreatedBy { get; set; }
    public DateTime DateUpdate { get; set; }
    public DateTime UpdateBy { get; set; }
    public List<VSTAttendance> MemberAttendance { get; set; }


}

HTML

@using (Html.BeginForm("Attendance", "Home", FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl, @class = "form-horizontal", role = "form" }))
{
    <div class="form-group">
        <label for="inputSpeaker" class="col-sm-2 control-label">
             Speaker</label>
        <div class="col-sm-6">
            @Html.TextBoxFor(m => m.Speaker, new { @class = "form-control", placeholder = "Speaker", type = "text", required = "required " })
        </div>
    </div>

<table class="table table-bordered table-condensed table-hover">
    <tr class="warning">
        <td></td>
        <td>Name</td>
        <td>Surname</td>
    </tr>
    @foreach (var item in Model.MemberAttendance)
    {
        <tr>
            <td>@Html.CheckBox("Attendant")</td>
            <td>@item.Name</td>
            <td>@item.Surname</td>
        </tr>
    }
    </table>

<div class="form-group">
    <div class="col-sm-10">
        <button type="submit" class="btn btn-info">
         <span class="glyphicon glyphicon-ok"></span>Submit</button>
     </div>
</div>

}

解决方案

You have to use for instead of foreach and use Hidden for . I have tested the following and works perfect

 @model ServiceAttendance

@{
    ViewBag.Title = "Attendance";
}

<h2>Attendance</h2>

@using (Html.BeginForm())
{
    <div class="form-group">
        <label for="inputSpeaker" class="col-sm-2 control-label">
            Speaker
        </label>
        <div class="col-sm-6">
            @Html.TextBoxFor(modelItem => modelItem.Speaker)
        </div>
    </div>

    <table class="table table-bordered table-condensed table-hover">
        <tr class="warning">
            <td></td>
            <td>Name</td>
            <td>Surname</td>
        </tr>
        @for (int i=0;i<Model.MemberAttendance.Count ;i++)
        {
            <tr>
                <td>@Html.CheckBoxFor(it=>Model.MemberAttendance[i].Attendant)
                   @Html.HiddenFor(it => Model.MemberAttendance[i].Attendant)
                </td>
                <td>@Model.MemberAttendance[i].Name
                    @Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td>
                <td>@Model.MemberAttendance[i].Surname
                    @Html.HiddenFor(it=>Model.MemberAttendance[i].Surname)</td>
            </tr>
        }
    </table>

    <div class="form-group">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-info">
                <span class="glyphicon glyphicon-ok"></span>Submit
            </button>
        </div>
    </div>

}

So what you have to do is replace the foreach with the following

 @for (int i=0;i<Model.MemberAttendance.Count ;i++)
            {
                <tr>
                    <td>@Html.CheckBoxFor(it=>Model.MemberAttendance[i].Attendant)
                       @Html.HiddenFor(it => Model.MemberAttendance[i].Attendant)
                    </td>
                    <td>@Model.MemberAttendance[i].Name
                        @Html.HiddenFor(it => Model.MemberAttendance[i].Name)</td>
                    <td>@Model.MemberAttendance[i].Surname
                        @Html.HiddenFor(it=>Model.MemberAttendance[i].Surname)</td>
                </tr>
            }

这篇关于使用 MVC Razor 传递列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆