简体   繁体   English

如何在ASP.NET MVC中使用带有引导程序的HTML标记从控制器获取结果?

[英]How I can use html tags with bootstrap in asp.net mvc for get a result from controller?

Hey Guys I have a problem with my ASP.NET MVC Application. 嗨,我的ASP.NET MVC应用程序有问题。 I want to make a Search textfield and a image button in my application. 我想在我的应用程序中创建一个搜索文本字段和一个图像按钮。 If I input a valu in the textfield I want use this value for search in a list and send the result to the View. 如果我在文本字段中输入一个值,则要在列表中使用该值进行搜索并将结果发送到视图。 I use bootstrap because it looks fine. 我使用引导程序,因为它看起来不错。

here is my code: Index.cshtml 这是我的代码:Index.cshtml

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Search", "HomeController"))
{
    <div class="container">
        <div class="row">
            <h2>Suche</h2>
            <div id="custom-search-input">
                <div class="input-group col-md-12">
                    <input type="text" id="txtSearch" class="  search-query form-control" placeholder="Search" name="txtSearch" />
                    <span class="input-group-btn">
                        <button class="btn btn-danger" type="submit">
                            <span class=" glyphicon glyphicon-search"></span>
                        </button>
                    </span>
                </div>
            </div>
        </div>
    </div>
}

Here is my Controller: 这是我的控制器:

[HttpPost]
public ActionResult Search(string txt)
{

    List<string> petList = new List<string>();
    petList.Add(txt);

    ViewBag.liste = petList;

    return View();
}

Your input does not have a name attribute that matches the parameter in the method so it is not bound. 您输入的name属性与方法中的参数不匹配,因此未绑定。 Change it to 更改为

<input type="text" id="txtSearch" class="..." name="txt" />

or change the method to match the current name attribute 或更改方法以匹配当前name属性

[HttpPost]
public ActionResult Search(string txtSearch)

Side note: Assuming your controller is named HomeController then it should be @using (Html.BeginForm("Search", "Home")) , not @using (Html.BeginForm("Search", "HomeController")) (which would only work if you have a controller named HomeControllerController ) 旁注:假设您的控制器名为HomeController那么它应该是@using (Html.BeginForm("Search", "Home")) ,而不是@using (Html.BeginForm("Search", "HomeController")) (仅当您有一个名为HomeControllerController的控制器时才有效)

暂无
暂无

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

相关问题 如何在没有Html.BeginForm的情况下使用html标记进行输入和在asp.net mvc 5中提交? - How I can use html tags for input and submit in asp.net mvc 5 without Html.BeginForm? 如何将数据从Select2选项/值(标签)获取到ASP.NET MVC中的控制器? - How do I get the data from a Select2 options/value (tags) to the controller in asp.net mvc? 从ASP.NET MVC控制器传递HTML到浏览器直接链接不会呈现HTML标签 - Passing HTML from ASP.NET MVC controller to browser direct link is not rendering HTML tags 如何获得ASP.NET MVC单选按钮以响应不区分大小写的路由,或来自控制器的指示? - How can I get ASP.NET MVC radio buttons to respond to a case insensitive route, or from directions from the controller? 如何在ASP.NET MVC中使用jQuery将html选择标记(下拉)值传递给控制器​​? - How can i pass a html select tag (dropdown) value to the controller using jquery in asp.net mvc? 如何在ASP.NET MVC 5.1项目中通过控制器的动作使用标准JSON格式器? - How can I use the standard JSON formatter from an action of my controller in an ASP.NET MVC 5.1 project? 我如何在ASP.NET MVC 3中创建asynchronus结果? - how i can make asynchronus result in ASP.NET MVC 3? 我怎么能用Lazy <T> 在ASP.NET MVC控制器? - How can i use Lazy<T> in an ASP.NET MVC Controller? 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 在ASP.NET MVC控制器中,如何获取Redirect(string URL)方法不附加“#”? - In an ASP.NET MVC Controller, how can I get the Redirect(string URL) method to not append a '#'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM