簡體   English   中英

如何將視圖中的對象綁定到控制器?

[英]How to bind an object in a view to the controller?

這可能是一個愚蠢的問題,但我對剃須刀有點陌生。 我正在嘗試創建動態表單。 我有一個字段對象列表,並在頁面中動態顯示它們。 但是,當我想保存字段的選定值作為下拉列表(示例)時,我不知道如何將foreach的對象保存到控制器中的模型中(我可以毫無傷害地保存值)。

Index.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>

BuildingBlockField:

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

HomeController的:

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

由於您要將ContractInformationsModel模型傳遞給視圖,該模型具有類型為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++
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM