简体   繁体   English

如何将字符串从View发布到ASP.NET MVC 5中的控制器?

[英]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". NET MVC 5,并具有一个视图“ Search.cshtml”和一个控制器“ HomeController”和一个模型“用户”。

I try to create a DataTable out of Active Directory and show this in a table in the view. 我尝试从Active Directory中创建一个DataTable并将其显示在视图的表中。

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 ;) 我如何将我的SearchString发布到我的控制器,然后我知道我必须做什么;)

Just specify action and controller name in the BeginForm method: 只需在BeginForm方法中指定动作和控制器名称:

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

Also I believe parameter names are case sensitive, so make sure textbox name corresponds to that of action parameter: 另外,我认为参数名称区分大小写,因此请确保文本框名称与action参数的名称相对应:

@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. 假设您没有任何特殊的路由设置,应该这样做-当用户单击“提交”按钮时,应触发“搜索”操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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