将数组的单个实例提交给控制器 [英] Submitting a single instance of an array to the controller
问题描述
我有一个像这样的视图模型:
I have a view model like:
public class OrganisationViewModel
{
public string OrganisationName { get; set; }
public List<BranchViewModel> Branches { get; set; }
// more properties...
}
public class BranchViewModel
{
public string BranchName { get; set; }
// more properties...
}
组织"页面的外观如下:
This is how the Organisation page looks like:
我要实现的是允许用户更新单个BranchViewModel
,因此我为每个分支创建了一个模态,当用户单击编辑分支"链接时,模态将打开:
What I want to achieve is to allow user to update a single BranchViewModel
, so I have created a Modal for each Branch and when user click on the Edit branch link, the modal will open:
@for (int i = 0; i < Model.BranchViewModels.Count(); i++)
{
var branchModalId = OrganisationHelper.GetBranchModalId(Model.BranchViewModels[i].BranchId);
<div class="modal fade" id="@branchModalId" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form action="/organisation/updateBranch" method="post" role="form">
<div class="modal-body">
@Html.AntiForgeryToken()
<div class="form">
<div class="form-group">
@Html.LabelFor(m => Model.BranchViewModels[i].BranchName, htmlAttributes: new { @class = "", @maxlength = GlobalConstants.MaxLengthForLongName })
@Html.TextBoxFor(m => Model.BranchViewModels[i].BranchName, new { @class = "form-control input-text" })
@Html.ValidationMessageFor(m => Model.BranchViewModels[i].BranchName, "", new { @class = "text-danger" })
</div>
@*more properties...*@
</div>
</div>
<div class="modal-footer">
<input type="button" value="Cancel" class="btn btn-secondary-grey" data-dismiss="modal" />
<input type="submit" class="btn btn-primary-action" value="Save" />
</div>
</form>
</div>
</div>
</div>
}
现在的问题是,由于分支属于数组,因此页面上的输入将生成为数组,因此如下所示:
Now the problem is, since branches belong to an array the inputs on the page are generated as an array, so something like this:
<input class="form-control input-text" data-val="true" id="BranchViewModels_0__BranchName" name="BranchViewModels[0].BranchName" readonly="readonly" type="text" value="35671900246">
因此,当我将更改提交到分支时,值将作为List
传递给控制器,因此该控制器将接受一个分支的列表:
So when I submit the changes to the branch, the values are passed to the controller as List
, so this controller will accept a List of one Branch:
public ActionResult UpdateBranch(List<BranchViewModel> branchViewModel)
{
}
我想要实现的是能够将单个分支传递给控制器,所以我希望控制器的签名像这样:
What I want to achieve is to be able to pass the single branch to the controller, so I want the signature of the controller to be like:
public ActionResult UpdateBranch(BranchViewModel branchViewModel)
{
}
但是我需要将分支显示为HTML中的数组,否则我将获得重复的输入ID ...实现此目的的最佳方法是什么?
But I need the branch to be rendered as an array in the HTML, otherwise I will get duplicated input Ids... what is the best way to achieve this?
推荐答案
ASP.NET MVC序列化可用于html元素名称.当您通过for循环创建索引器时,索引器将带有带有_i__的html元素名称(其中i是该集合中分支的索引). 因此,您可以尝试使用foreach,而不是使用索引器(通过for循环访问Model.BranchViewModels [i]).
ASP.NET MVC serialization works on html element names. Indexers will have html element names with _i__ (where i is index of branch in that collection) when you create them via for loop. So, instead of using indexers (accessing Model.BranchViewModels[i] via for loop), you can try using foreach.
@{
var idx = 0;
}
@foreach (var branch in Model.BranchViewModels)
{
var branchModalId = OrganisationHelper.GetBranchModalId(branch.BranchId);
<div class="modal fade" id="@branchModalId" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form action="/organisation/updateBranch" method="post" role="form">
<div class="modal-body">
@Html.AntiForgeryToken()
<div class="form">
<div class="form-group">
@Html.LabelFor(m => branch.BranchName, htmlAttributes: new { @class = "", @maxlength = GlobalConstants.MaxLengthForLongName })
@Html.TextBoxFor(m => branch.BranchName, new { @class = "form-control input-text", @id= Model.BranchViewModels[idx].BranchName})
@Html.ValidationMessageFor(m => branch.BranchName, "", new { @class = "text-danger" })
</div>
@*more properties...*@
</div>
</div>
<div class="modal-footer">
<input type="button" value="Cancel" class="btn btn-secondary-grey" data-dismiss="modal" />
<input type="submit" class="btn btn-primary-action" value="Save" />
</div>
</form>
</div>
</div>
</div>
idx++;
}
这篇关于将数组的单个实例提交给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!