[英]How do I post back all selected items in a multi-select list so it is included in my view model?
I have an ASP.Net Core 2.1 MVC website I am working on. 我有一个正在工作的ASP.Net Core 2.1 MVC网站。
In one of my views, I have set up two multi select lists to allow the client to assign users to a site by providing a list of all unassigned users in the left hand list and a list of assigned users on the right hand list, using Esua Silva's example for html/javascript/scss. 在我的一种观点中,我已经建立了两个多选列表,以允许客户端通过使用左手列表中所有未分配用户的列表和右手列表中的已分配用户的列表来分配用户到站点。 Esua Silva的 html / javascript / scss示例。 This is working just fine.
这很好。
For the post back, I realized that I need to select all of the items in the new "Assigned Users" list at submit so I am using javascript to set all of the options in the "Assigned Users list to true when the user clicks the update (submit) button. 对于回发,我意识到我需要在提交时选择新的“分配的用户”列表中的所有项目,因此当用户单击“添加的用户”列表时,我将使用javascript将“分配的用户”列表中的所有选项设置为true。更新(提交)按钮。
However, I am not getting the list of users in my model so I can work with them in the controller on post back. 但是,我没有得到模型中的用户列表,因此可以在回发时在控制器中使用它们。 The list of assigned users in the model always has a count of 0 when it is passed into the controllers POST method.
当模型中的分配用户列表传递到控制器的POST方法中时,其计数始终为0。
Here is my multi-select list HTML in the view. 这是视图中的多选列表HTML。
<!-- User Assignment Tab -->
<div role="tabpanel" class="tab-pane" id="assignUsersTab" aria-labelledby="assign-users-tab">
<div class="card-body ml3 mr3">
<div class="row">
<div class="col-5">
<label>Users Assigned to Site</label>
<select multiple="multiple" id="lstBox1" name="AssignedUserList" size="10" asp-items="@(new SelectList(Model.AssignedUserList, "UserId", "Name"))" class="form-control">
</select>
</div>
<div class="col-2 text-center subject-info-arrows pt-5">
<input type="button" id="btnAllRight" value=">>" class="btn btn-default"/><br/>
<input type="button" id="btnRight" value=">" class="btn btn-default"/><br/>
<input type="button" id="btnLeft" value="<" class="btn btn-default"/><br/>
<input type="button" id="btnAllLeft" value="<<" class="btn btn-default"/>
</div>
<div class="col-5">
<label>Unassigned Company Users</label>
<select multiple="multiple" id="lstBox2" size="10" name="UnassignedUserList" asp-items="@(new SelectList(Model.UnassignedUserList, "UserId", "Name"))" class="form-control"></select>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
Here is my view model. 这是我的视图模型。
public class EditSiteViewModel
{
public int Id { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 2)]
[Display(Name = "Site Name")]
public string SiteName { get; set; }
[Required]
[Display(Name = "Company ID")]
public int CompanyId { get; set; }
[Display(Name = "Company Name")]
public string CompanyName { get; set; }
[Display(Name = "App ID")]
public string AppId { get; set; }
[Display(Name = "App Key")]
public string AppKey { get; set; }
[Display(Name = "Admin Site")]
public bool IsAdminSite { get; set; }
[Display(Name = "Enabled")]
public bool IsEnabled { get; set; }
public string ReturnUrl { get; set; }
public List<Company> CompanyList { get; set; }
public List<UserSiteItem> UnassignedUserList { get; set; }
public List<UserSiteItem> AssignedUserList { get; set; }
public List<ApplicationUser> CompanyUsersList { get; set; }
}
Here is my jquery for the SelectAll operation when the user clicks the Update button 这是当用户单击“更新”按钮时我对SelectAll操作的查询
$('#site-form').on("submit", selectAll);
function selectAll() {
$("#lstBox1").find("option").each(function() {
$(this).attr('selected', true);
});
console.log(`SelectAll processed`);
}
I can see the console.log is getting hit when I F12 in Chrome. 我可以看到当我在Chrome中按F12键时console.log受到了攻击。
Here is my POST edit method signature in my controller. 这是我的控制器中的POST编辑方法签名。
[HttpPost]
public async Task<IActionResult> Edit(EditSiteViewModel editSite, string cancel)
When I examine the properties of the EditSiteViewModel upon submit, I see that the AssignedUserList property has a count of 0. I can see the other properties in my view model so I know the form post stuff is working, just not for the multi-select list. 当我在提交时检查EditSiteViewModel的属性时,我看到AssignedUserList属性的计数为0。我可以在视图模型中看到其他属性,因此我知道表单发布的内容正在工作,但不适用于多选名单。
I assume that the issue is with how I am trying to associate the multi-select list with the viewmodel property. 我认为问题在于我如何尝试将多选列表与viewmodel属性相关联。 I have seen SO posts that say I need to put an [] after the text in the name.
我看到过这样的帖子,说我需要在名称中的文本后面加上[]。 (ie name="AssignedUserList[]" in the
(即,“ name =“ AssignedUserList []”
I have spent many hours looking for a solution with no luck. 我花了很多时间寻找没有运气的解决方案。 Any help wou.d be greatly appreciated.
任何帮助将不胜感激。
The posted value of a select, will always be primitive types (string, int, etc.). select的发布值将始终是原始类型(字符串,整数等)。 Accordingly, the posted value of a select multiple will simply be an enumerable of primitive types.
因此,选择倍数的过帐值将简单地是原始类型的枚举。 As such, you cannot bind directly to something like
List<UserSiteItem>
. 因此,您不能直接绑定到诸如
List<UserSiteItem>
类的东西。 Instead, you need to bind to a property of type List<string>
or List<int>
(depending on whether you've customized identity's user PK), since in this case, it will be a list of user ids being posted back. 相反,您需要绑定到
List<string>
或List<int>
类型的属性(取决于您是否已自定义身份的用户PK),因为在这种情况下,它将是回传的用户ID的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.