简体   繁体   中英

How I can post a String from View to the Controller in ASP.NET MVC 5?

net mvc 5 and have a view "Search.cshtml" and a controller "HomeController" and a model "user".

I try to create a DataTable out of Active Directory and show this in a table in the view.

I can show the table in the view but i want to implement a search textfield for selection a value in this table.

For this I create a method for testing and have the Method in my controller:

public ActionResult Search(string searchString){
    var data = GetTable(searchstring);
    return View(data);
}

static DataTable GetTable(string data){
   //...here i get data from the active Directory and filter with data parameter
}

And here is my View

<h2>Suchen</h2>

@using(HTML.BeginForm()){

   @Html.TextBox("SearchString") 

   <input type="submit" value="Suchen">

   <table>
     <thead>
       <tr>
         @foreach(DataColumn col in MOdel.Columns){
                     <th>@col.ColumnName</th>
         }
       </tr>
     </thead>
     <tbody>
      @foreach(DataRow col in Model.Rows){
         <tr>
           @foreach(DataColumn col in Model.Columns){
            <td>@row[col.columnName]</td>
           }
         </tr>
       }
     </tbody>
   </table>

}

How I can post my SearchString to my Controller and then I know wht i must do ;)

Just specify action and controller name in the BeginForm method:

@using(Html.BeginForm("Search", "Home")){

Also I believe parameter names are case sensitive, so make sure textbox name corresponds to that of action parameter:

@Html.TextBox("searchString")

Assuming you do not have any special routing setup, that should do it - when user clicks submit button, "Search" action should get triggered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM