首页 > 解决方案 > 如何将视图中的对象绑定到控制器?

问题描述

这可能是一个愚蠢的问题,但我对剃须刀有点陌生。我正在尝试创建一个动态表单。我有一个字段对象列表,并在我的页面中动态显示它们。但是,当我想将我的字段的选定值保存为下拉列表(示例)时,我不知道如何将我的对象保存foreach到控制器中的模型中(我可以保存我的值而不会造成伤害)。

索引.cshtml:

<div class="row">
    @foreach (var buildingBlock in buildingBlocks)
    {
        <div class="col-sm">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">@buildingBlock.BuildingBlockTitle</h5>
                    @foreach (Test.Test.Models.BuildingBlockField buildingBlockField in buildingBlockFields)
                    {
                    <div class="form-group">
                        <label for="companyName">Company Name</label>
                        //I tried that but it's not working (Obviously :))
                        @Html.EditorFor(model => buildingBlockField)
                        @Html.DropDownListFor(model => model.buildingBlockFields[0].Values, buildingBlockField.OptionDetails, "Select Contract", new { @class = "selectpicker", multiple = "multiple" })
                    </div>
                    }
                </div>
            </div>
        </div>
    }
</div>

构建块字段:

public class BuildingBlockField
{
    public int BuildingBlockFieldID{ get; set; }
    public int BuildingBlockID { get; set; }
    public List<SelectListItem>? OptionDetails { get; set; }
    public string FieldTitle { get; set; }
    public FieldType Type { get; set; }
    public bool IsMultiple { get; set; }

    public int[] Values { get; set; }
    public string Value { get; set; }
}

模型控制器:

public class ContractInformationsModel
{
    public List<BuildingBlockField> buildingBlockFields { get; set; }
}

家庭控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.BuildingBlocks = Models.BuildingBlock.getBuildingBlocks();
        ViewBag.BuildingBlockFields = Models.BuildingBlockField.getBuildingBlockFields();

        return View();
    }

    [HttpPost]
    public ActionResult generateWordContract(ContractInformationsModel contractInformations)
    {
        return View("Index");
    }
}

我希望在我的控制器对象contractInformations中找到一个包含所有信息而不仅仅是值的 buildingBlockFields 列表。

谢谢

编辑 :

这似乎可行,但我必须为每个属性都这样做,然后隐藏。还有其他解决方案吗?

                        @for (var i = 0; i < buildingBlockFields.Count(); i++){
                        <div class="form-group">
                            @Html.HiddenFor(model => model.buildingBlockFields[i].BuildingBlockFieldID, new { Value = buildingBlockFields[i].BuildingBlockFieldID })
                            @Html.HiddenFor(model => model.buildingBlockFields[i].FieldTitle, new { Value = buildingBlockFields[i].FieldTitle })
                            @Html.HiddenFor(model => model.buildingBlockFields[i].Type, new { Value = buildingBlockFields[i].Type })
                            @Html.DropDownListFor(model => model.buildingBlockFields[0].Values, buildingBlockFields[i].OptionDetails, "Select Contract", new { @class = "selectpicker", multiple = "multiple" })
                        </div>
                    }

标签: c#asp.netasp.net-mvc

解决方案


由于您将ContractInformationsModel模型传递给具有 type 列表的视图,因此BuildingBlockField您的 html 应该包含构建块字段 ID 和一个可以识别该列表中的索引的“计数器”。

@{
  // declare counter
  int i = 0
}
@foreach (BuildingBlockField buildingBlockField in buildingBlockFields)
{
     <div class="form-group">
       <label for="companyName">@buildingBlockField.FieldTitle</label>
       @Html.HiddenFor(model=>model.buildingBlockFields[i].BuildingBlockFieldID)
       @Html.TextBoxFor(model => model.buildingBlockFields[i].FieldTitle, new { @class = "form-control", Value = @buildingBlockField.FieldTitle })
       @Html.DropDownListFor(model => model.buildingBlockFields[i].Values, buildingBlockField.OptionDetails, "Select Contract", new { @class = "selectpicker", multiple = "multiple" })
     </div>

     @i++
}

推荐阅读