我有以下 Ajax Begin 表单:-
@using (Ajax.BeginForm("AssignUsers", "SecurityGroup",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "UsersAssignment"//,
// LoadingElementId = "progress",
// OnSuccess = "reenable"
}))
{
@Html.HiddenFor(Model => Model.GroupID)
@Html.AntiForgeryToken()
<p>Search <input placeholder="Search by name.." name="selectedUserNames" type="text" data-autocomplete-source= "@Url.Action("AutoComplete", "SecurityGroup")" /> </p>
<input type="submit" value="Search" />
}
但问题是用户没有在“测试输入”字段中输入任何值,然后一个空字符串将被传递到数据库。那么我如何检查数组是否包含空字符串。
将处理视图的操作方法是:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AssignUsers(int GroupID, string[] selectedUserNames, string[] currentUserNames)
{ try
{if (ModelState.IsValid)
{repository.AssignUserGroup(GroupID, selectedUserNames, currentUserNames);
repository.Save();
if (!Request.IsAjaxRequest())
{
return RedirectToAction("Details", new { id = GroupID });
}
else if (Request.IsAjaxRequest())
{var ADUsers = repository.GetADUsers(null);
var group = repository.FindAllGroup(GroupID);
PopulateAssignedUsersData(group, ADUsers);
return PartialView("_Group", group);
并且存储库方法是:-
public void AssignUserGroup(int id, string[] selectedUsers, string[] currentusernames)
{
var usergroups = tms.UserGroups.Where(a=>a.GroupID == id);
foreach (var ug in usergroups)
{
if (currentusernames != null)
{for (int c = 0; c < currentusernames.Count(); c++)
{if (ug.UserName == currentusernames[c])
{tms.UserGroups.Remove(ug);
}}}}
if( selectedUsers !=null){
for (int i = 0; i < selectedUsers.Count(); i++)
{UserGroup usergroup = new UserGroup();
usergroup.GroupID = id;
usergroup.UserName = selectedUsers[i];
tms.UserGroups.Add(usergroup);} } }