c# - 如何将视图中的对象绑定到控制器?
问题描述
这可能是一个愚蠢的问题,但我对剃须刀有点陌生。我正在尝试创建一个动态表单。我有一个字段对象列表,并在我的页面中动态显示它们。但是,当我想将我的字段的选定值保存为下拉列表(示例)时,我不知道如何将我的对象保存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>
}
解决方案
由于您将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++
}
推荐阅读
- multithreading - ExecutorService 使用 Stream 时不并行执行线程
- javascript - 如何预防
导出 javascript DataTable 时显示 - android - FT232R 设备的 USB 串口的 Android requestWait 没有被阻塞
- python-3.x - 多页上的 Tkinter 串行数据
- shell - 杀死一个 Docker 容器
- c# - 给定从 0 开始的字符串绝对索引,确定从 1 开始的行号和列号(C#)
- api - 哪个聊天api的搜索功能好?
- arrays - 如何将一列数组转换为Spark中每个数组之和的列?
- apache-spark - JDBC - cassandra sparksql 连接器可以在查询工具(即 Tableau/Alteryx/Sqlclient)中加入吗?
- java - 如何在 Rxjava 中按键合并两个 Observable?