如何从视图到控制器返回viewmodel的列表数据

时间:2017-02-01 10:09:52

标签: c# asp.net-mvc razor

我创建了包含列表的viewmodel,此列表用于查看复选框。但是当我点击提交时,我会获得除此列表之外的所有viewmodel数据。此列表将保持为空。 这是我的代码示例,

  @model project.viewmodel
  @using (Html.BeginForm())
  {

   @Html.AntiForgeryToken()

    <div class="form-horizontal">

    <div class="form-group">
        @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Id, new Dictionary<string, object> { { "class", "form-control ui-widget" }, { "readonly", "readonly" } })
            @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-4">
            @Html.TextBoxFor(model => model.Name, new Dictionary<string, object> { { "class", "form-control ui-widget" }, { "readonly", "readonly" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Occupation</label>
        <div class="col-md-4">
            @if (Model.occupationList != null)
            {
                foreach (var z in Model.occupationList)
                {
                    @Html.HiddenFor(z => z.Id)
                    @Html.HiddenFor(z => z.Name)
                    @Html.CheckBoxFor(z => z.IsSelected)
                    @Html.DisplayFor(z => z.Name)
                }
            }
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-success" />
        </div>
    </div>

内部视图,

let somethingTrue = true
[1,2,3,4,5].map(i=>somethingTrue && i*2)

当我点击提交时,在conroller里面我只得到了OccupList为null,其他数据都可以得到。 有人可以解释我的错误。关于mvc我是全新的。谢谢你们。

0 个答案:

没有答案