没有模型的MVC 5 Html.BeginForm

时间:2014-09-09 13:07:10

标签: c# asp.net-mvc razor

我的_Layout页面上有一个简单的搜索表单。

如何轻松地将search-fld中的值传递给控制器​​?无需创建模型或ViewModel。

@using (Html.BeginForm("Search", "Home", new { id = "search-fld" }, FormMethod.Post, new { @class = "header-search pull-right" }))
{
    @Html.AntiForgeryToken()

    <input type="text" placeholder="Search" id="search-fld">
    <button type="submit">
        <i class="fa fa-search"></i>
    </button>
}

如果我运行这个,那么“search-fld”会发送到控制器(offcourse)

使用Ajax表单并使用Jquery获取值?

2 个答案:

答案 0 :(得分:14)

只需提供input name属性:

<input type="text" placeholder="Search" id="search-fld" name="searchValue">

然后将该名称与控制器HttpPost方法中的参数进行匹配:

[HttpPost]
public ActionResult Search(string searchValue)

答案 1 :(得分:0)

你可以像这样使用Ajax调用:

 $(document).ready(function() {
$("#yourButton").click(function () {
            var yourValue = $("#search-fld").val() ;
            var url = "Search/asd/";
            $.ajax({
                url: url,
                cache: false,
                type: 'POST',
                data: {
                    searchValue = yourValue
                }
                success: function (data) {
                    if (data === true) {
                        alert('Success');
                    } else {
                        alert('Failed');
                    }
                    }
                })
            })
        })

和控制器中的方法:

[HttpPost]
public ActionResult asd(string searchValue)
{

}
相关问题