2013-07-16 79 views
-1

我有以下的阿賈克斯開始形式: -如何檢查如果輸入值是空

@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" /> 
} 

但問題是用戶沒有在測試輸入字段中輸入任何值,則空sting將傳遞給數據庫。所以我如何檢查數組是否包含空字符串。

將處理視圖的操作方法是: -

[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);} } } 

回答

1

你可以做這樣的事情:

if(string.IsNullOrWhiteSpace (your string; for example : currentusernames[c])) 
{ 
    -- do something or throw an exception-- 
} 

else 
{ 
    -- do something else -- 
} 
0

您需要在某個時候驗證您的輸入。這通常在模型中完成,然後檢查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); 
     } 
    } 
} 

然後更新您的控制器以處理各種預期的異常。

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 
} 
相關問題