在类中搜索并根据用户ID /文本框显示表中的类

时间:2012-09-25 16:45:22

标签: c# html asp.net-mvc-3

Aspx引擎

我有一个网络表单,要求用户输入用户ID。用户标识用于搜索类并显示与用户标识匹配的类。

现在出现的问题是,当我输入用户ID时,会显示所有类。只有类才会被显示出来。具有在文本框中输入的userId的类

HTML

<div align="center">
    <form id="searchUser" method="post" action="Search">
        <table align="center">
    <tr>
        <td class="label">
            Enter ID:
        </td>
        <td>
            <input type="text" name="UserId" id="UserId" value="<%=(string)(ViewBag.userid)%>" />
        </td>
    </tr>
      <tr style ="display:none;">
        <td class="label">
            Email:
        </td>
        <td>
            <input type="text" name="Email" id="Text1" value="<%=(string)(ViewBag.email)%>" />
        </td>
    </tr>
      <tr style ="display:none;">
        <td class="label">
            lastFourDigits:
        </td>
        <td>
            <input type="text" name="LastFourDigits" id="Text2" value="<%=(string)(ViewBag.lastFourDigits)%>" />
        </td>
    </tr>
    <tr>
        <td>
            <button class="searchButton" id="searchButton">Search</button>
        </td>
    </tr>
  </table>
 </form>
  </div>
  <hr /> 

  <% if (ViewBag.searchClass !=null)
    { %>
    <h2>Search Resuls</h2>
    <br />
    <%List<AAlexUsers.Models.SearchClass> searchClassList= ViewBag.searchClass;%>
    <%if(searchClassList.Count>0)
    <table>
          <%foreach (AAlexUsers.Models.SearchClass searchClass in searchClassList)
     {%>
      <tr>
         <td>
            UserID:
        </td>
        <td class="content">
          <%=searchClass.userId%>
        </td>
    </tr>
    <tr>
        <td>
            Email:
        </td>
        <td class="content">
         <%=searchClass.email%>
        </td>
    </tr>
    <tr>
        <td>
        Last Four Digits:
        </td>
        <td class="content">
          <%=searchClass.lastFourdigits%>
         </td>
    </tr>
    <%} %>
    <%} %>
 </table>

    <%} else %>
 <%{ %>
    <h2>No Class found.</h2>
 <%} %>

控制器

 public ActionResult Search()
    {
        string userId = Request["UserId"];
        string email = Request["Email"] ?? string.Empty;
        string lastFourdigits = Request["LastFourDigits"] ?? string.Empty;

        bool view = false;

        if (string.IsNullOrEmpty(userId))
        {
            view = true;
        }
        if (!view)
        {
            List<AAlexUsers.Models.SearchClass> searchClass = AAlexUsers.Models.SearchClass.Users(userId,email,lastFourdigits);
            {
                ViewBag.searchClass = searchClass;
                ViewBag.lastFourdigits = lastFourdigits;
                ViewBag.userId = userId;
                ViewBag.email = email;


            }
        }
        return View();
    }

模型

  public class SearchClass
{
    public string userId { get; set; }
    public string email { get; set; }
    public string lastFourdigits { get; set; }

    public static List<AAlexUsers.Models.SearchClass>Users(string userId, string email, string lastFourdigits)   
    {
        SearchClass Alex = new SearchClass();
        Alex.userId = "1234";
        Alex.email = "Alex@gmail.com";
        Alex.lastFourdigits = "1885";

        SearchClass Emilio = new SearchClass();
        Emilio.userId = "0928";
        Emilio.email = "Cubano@gmail.com";
        Emilio.lastFourdigits = "0706";

        SearchClass Ulysses = new SearchClass();
        Ulysses.userId = "0914";
        Ulysses.email = "lysses@gmail.com";
        Ulysses.lastFourdigits = "01zx";

        var list = new List<SearchClass>();
        list.Add(Alex);
        list.Add(Emilio);
        list.Add(Ulysses);

        return list;

    }

1 个答案:

答案 0 :(得分:2)

我希望这能指出你正确的方向。

public class SearchClass
{
    public string userId { get; set; }
    public string email { get; set; }
    public string lastFourdigits { get; set; }

    public static List<SearchClass> Users(string userId, string email, string lastFourdigits)   
    {
        SearchClass Alex = new SearchClass();
        Alex.userId = "1234";
        Alex.email = "Alex@gmail.com";
        Alex.lastFourdigits = "1885";

        SearchClass Emilio = new SearchClass();
        Emilio.userId = "0928";
        Emilio.email = "Cubano@gmail.com";
        Emilio.lastFourdigits = "0706";

        SearchClass Ulysses = new SearchClass();
        Ulysses.userId = "0914";
        Ulysses.email = "lysses@gmail.com";
        Ulysses.lastFourdigits = "01zx";

        var list = new List<SearchClass>();
        list.Add(Alex);
        list.Add(Emilio);
        list.Add(Ulysses);

        IEnumerable<SearchClass> result = list;
        if(!string.IsNullOrEmpty(userId))
            result = result.Where(u => u.userId == userId);
        if(!string.IsNullOrEmpty(email))
            result = result.Where(u => u.email == email);
        if(!string.IsNullOrEmpty(lastFourdigits))
            result = result.Where(u => u.lastFourdigits == lastFourdigits);

        return result.ToList();
    }