C# 如何检查输入值是否为空
我有以下Ajax Begin表单:-C# 如何检查输入值是否为空,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下Ajax Begin表单:- @using (Ajax.BeginForm("AssignUsers", "SecurityGroup", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "UsersAssignment"//, // LoadingElementId = "progress", /
@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" />
}
存储库方法是:-
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);} } }
public void AssignUserGroup(int-id,string[]selectedUsers,string[]currentusernames)
{
var usergroups=tms.usergroups.Where(a=>a.GroupID==id);
foreach(用户组中的变量ug)
{
if(currentusernames!=null)
{for(int c=0;c
您可以执行以下操作:
if( string.IsNullOrWhiteSpace ( your string; for example : currentusernames[c] ) )
{
-- do something or throw an exception--
}
else
{
-- do something else --
}
您需要在某个时候验证您的输入。这通常通过模型完成,然后检查
ModelState
。但是,由于生成了复杂的属性,因此最好进一步验证程序中的输入
更新存储库方法以验证提供给它的参数
public void AssignUserGroup(int id, string[] selectedUsers, string[] currentusernames)
{
if (id < 1) throw new ArgumentExpcetion("id");
if (selectedUsers.Length == 0) throw new ArgumentException("selectedUsers");
if (currentusernames.Length == 0) throw new ArgumentException("currentusernames");
var usergroups = tms.UserGroups.Where(a=>a.GroupID == id);
// May need to user .Count instead of .Length
if (usergroups == null || usergroups.Length == 0) throw new ArgumentOutOfRangeException("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);
}
}
}
public void AssignUserGroup(int id, string[] selectedUsers, string[] currentusernames)
{
if (id < 1) throw new ArgumentExpcetion("id");
if (selectedUsers.Length == 0) throw new ArgumentException("selectedUsers");
if (currentusernames.Length == 0) throw new ArgumentException("currentusernames");
var usergroups = tms.UserGroups.Where(a=>a.GroupID == id);
// May need to user .Count instead of .Length
if (usergroups == null || usergroups.Length == 0) throw new ArgumentOutOfRangeException("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);
}
}
}
try
{
repository.AssignUserGroup(GroupID, selectedUserNames, currentUserNames);
} catch (Exception e)
{
if (e is ArgumentException || e is ArgumentOutOfRangeException)
{
return redirect("Error");// Show an error view or message etc
}
throw; // Allow other exceptions to bubble
}