Asp.net mvc ASP.net MVC 3处理“我的表格”每行中的多个复选框

Asp.net mvc ASP.net MVC 3处理“我的表格”每行中的多个复选框,asp.net-mvc,html.checkbox,Asp.net Mvc,Html.checkbox,我正在为猜字游戏建立一个管理员界面,这样管理员用户可以通过选择游戏中出现的单词,然后选择单词中的哪些字母将被编码来设置游戏 MainGameTable编辑页 @model GameServer.ViewModels.GameTableModel @section Javascript { <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></scr

我正在为猜字游戏建立一个管理员界面,这样管理员用户可以通过选择游戏中出现的单词,然后选择单词中的哪些字母将被编码来设置游戏

MainGameTable编辑页

@model GameServer.ViewModels.GameTableModel

@section Javascript
{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"    type="text/javascript"></script>
}



@{
    ViewBag.Title = "GameTableEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>GameTableEdit</h2>

@using (Html.BeginForm("GameTableEdit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>GameTable</legend>
        @Html.HiddenFor(model => model.GameTableId)
        @Html.HiddenFor(model => model.GameTableNumber)

        <div class="editor-label">
            Table #: @Html.DisplayFor(model => model.GameTableNumber)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.SubjectId)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.SubjectId, new SelectList(Model.Subjects, "Key", "Value"))
            @Html.ValidationMessageFor(model => model.SubjectId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ComplexityId)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.ComplexityId, new SelectList(Model.Complexities, "Key", "Value"))
            @Html.ValidationMessageFor(model => model.ComplexityId)
        </div>

        <button type="submit" name="button" value="GetWords">Get Words</button>

                @Html.Partial("GameMatrix/_LineWordsTable", Model)

         <p>
           <button type="submit" name="button" value="Save">Save</button>
         </p>

    </fieldset>
}



<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@model GameServer.ViewModels.gamestablemodel
@节Javascript
{
}
@{
ViewBag.Title=“GameTableEdit”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
游戏表编辑
@使用(Html.BeginForm(“GameTableEdit”、“Admin”、FormMethod.Post、new{enctype=“multipart/formdata”}))
{
@Html.ValidationSummary(true)
游戏桌
@Html.HiddenFor(model=>model.GameTableId)
@Html.HiddenFor(model=>model.GameTableNumber)
表#:@Html.DisplayFor(model=>model.GameTableNumber)
@LabelFor(model=>model.SubjectId)
@DropDownListFor(model=>model.SubjectId,新选择列表(model.Subjects,“Key”,“Value”))
@Html.ValidationMessageFor(model=>model.SubjectId)
@LabelFor(model=>model.ComplexityId)
@DropDownListFor(model=>model.ComplexityId,新选择列表(model.Complexities,“Key”,“Value”))
@Html.ValidationMessageFor(model=>model.ComplexityId)
获得话语权
@Html.Partial(“GameMatrix/_LineWordsTable”,模型)

拯救

} @ActionLink(“返回列表”、“索引”)
表格中每个单词的部分页码

@model GameServer.ViewModels.GameTableModel
@if (Model.SelectedLinewords.Count != null && Model.SelectedLinewords.Count > 0)
{ 

    <table>
        <tr>
            <th>
                Select Word
            </th>
            <th>
                LineWord
            </th>
            <th>
                Characters to Display
            </th>
        </tr>


                    @Html.EditorFor(x => x.SelectedLinewords)


    </table>

}
@model GameServer.ViewModels.gamestablemodel
@如果(Model.SelectedLinewords.Count!=null&&Model.SelectedLinewords.Count>0)
{ 
选择单词
行话
要显示的字符
@Html.EditorFor(x=>x.SelectedLinewords)
}
每行的编辑器模板:

@model GameServer.ViewModels.SelectedLineWord
  <tr>   
<td>
    @Html.CheckBoxFor(x => x.isSelected)
</td> 
<td>
    @Html.DisplayFor(x => x.LineWord)
</td>     
<td>

    @Html.HiddenFor(x=>x.LineWordId)
    @Html.HiddenFor(x=>x.LineWord)
    @{ char[] lineword = Model.LineWord.ToCharArray(); }

    @for (int i = 0; i < Model.LineWord.Length; i++) 
    {

        <input type="checkbox" name="DisplayCharPosition"  value="@i" /> @lineword[i]
    }



</td>
</tr>
@model GameServer.ViewModels.SelectedLineWord
@CheckBoxFor(x=>x.isSelected)
@DisplayFor(x=>x.LineWord)
@Html.HiddenFor(x=>x.LineWordId)
@Html.HiddenFor(x=>x.LineWord)
@{char[]lineword=Model.lineword.ToCharArray();}
@for(int i=0;i
这是我的视图模型

public class SelectedLineWord 
    {
        [Required]
        public Guid LineWordId { get; set; }

        [Required]
        public String LineWord { get; set; }


        public int[] DisplayCharPosition { get; set; }

        [Required]
        public bool isSelected { get; set; }

        public SelectedLineWord()
        {

        }

        public SelectedLineWord(Guid linewordid, String word, String displaycharposition)
        {
            LineWordId = linewordid;
            LineWord = word;
            String[] pos = displaycharposition.Split(',');

            DisplayCharPosition = new int[word.Length];

            for (int i = 0; i < word.Length; i++)
            {
                DisplayCharPosition[i] = 0;
            }

            for (int i = 0; i < pos.Length; i++)
            {
                DisplayCharPosition[Int32.Parse(pos[i])] = 1;
            }
        }

        public SelectedLineWord(Guid linewordid, String word, bool issel)
        {
            LineWordId = linewordid;
            LineWord = word;
            isSelected = issel;
        }

    } 

    public class GameTableModel
    {

        [Required]
        public Guid GameTableId { get; set; }

        [Required]
        public Guid GameMatrixId { get; set; }


        [Required]
        [Display(Name = "Table Subject")]
        public int SubjectId { get; set; }

        [Required]
        [Display(Name = "Minimum Complexity")]
        public int ComplexityId { get; set; }


        [Required]
        public int GameTableNumber { get; set; }

        [Required]
        [Display(Name = "Include a Bonus table")]
        public bool IsBonus { get; set; }

        [Display(Name = "Table Subject")]
        public Dictionary<int, string> Subjects;

        [Display(Name = "Minimum Complexity")]
        public Dictionary<int, int> Complexities;

        public List<GameTableLine> GameTableLines { get; set; }
        public List<SelectedLineWord> SelectedLinewords { get; set; }


        public GameTableModel ()
        {
            try
            {

                //get a connection to the database
                var data = new GameServerDataModelDataContext();

                //Fetch the subjects reference data
                var subjects = from c in data.Subjects orderby c.Subject1 select new { c.SubjectId, c.Subject1};

                Subjects = new Dictionary<int, string>();

                foreach (var subject in subjects)
                {
                    Subjects.Add(subject.SubjectId, subject.Subject1);
                }

                //Fetch the complexities questions
                Table<Complexity> dComplexities = data.GetTable<Complexity>();

                Complexities = new Dictionary<int, int> { { 0, 0 } };
                foreach (var complexity in dComplexities)
                {
                    if (complexity.Complexity1 != null)
                        Complexities.Add(complexity.ComplexityId, (int)complexity.Complexity1);
                }

            }
            catch (Exception ex)
            {
                //[TODO: Complete the exception handeling code.]
            }
        }
    }
public类SelectedLineWord
{
[必需]
公共Guid LineWordId{get;set;}
[必需]
公共字符串行字{get;set;}
public int[]DisplayCharPosition{get;set;}
[必需]
公共布尔值被选为{get;set;}
公共选择的LineWord()
{
}
public SelectedLineWord(Guid linewordid、String word、String displaycharposition)
{
LineWordId=LineWordId;
LineWord=单词;
字符串[]pos=displaycharposition.Split(',');
DisplayCharPosition=新整数[字长];
for(int i=0;i
我的问题是,当我点击save按钮时,传递给控制器的模型已正确填充了所有内容,但为DisplayCharPosition选择的复选框返回null。我所期望的是一个int[],填充了所选显示字符的索引

有人能帮我理解我做错了什么吗

我已经设法解决了这个问题(但我仍然愿意接受一些关于更好的建议)
public class SelectedLineWord 
    {
        [Required]
        public Guid LineWordId { get; set; }

        [Required]
        public String LineWord { get; set; }


        public bool[] DisplayCharPosition { get; set; }

        [Required]
        public bool isSelected { get; set; }

        public SelectedLineWord()
        {

        }

        public SelectedLineWord(Guid linewordid, String word, String displaycharposition)
        {
            LineWordId = linewordid;
            LineWord = word;
            String[] pos = displaycharposition.Split(',');

            DisplayCharPosition = new bool[word.Length];

            //set all to false
            for (int i = 0; i < word.Length; i++)
            {
                DisplayCharPosition[i] = false;
            }

            //now only set the ones that were already in the db.
            for (int i = 0; i < pos.Length; i++)
            {
                DisplayCharPosition[Int32.Parse(pos[i])] = true;
            }
        }