[英]MVC 5 Html.BeginForm without model
I have a simple search form on my _Layout page. 我在_Layout页面上有一个简单的搜索表单。
How can I easily pass the value from search-fld to the controller? 如何轻松地将search-fld中的值传递给控制器? Without having to create a Model or ViewModel. 无需创建Model或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>
}
If I run this then "search-fld" get send to the controller (offcourse) 如果我运行这个,那么“search-fld”会被发送到控制器(offcourse)
Use Ajax form instead and get the value with Jquery? 使用Ajax表单并使用Jquery获取值?
Simply by giving your input
a name
attribute: 只需为input
提供name
属性:
<input type="text" placeholder="Search" id="search-fld" name="searchValue">
Then matching that name with a parameter in your controller HttpPost
method: 然后将该名称与控制器HttpPost
方法中的参数进行匹配:
[HttpPost]
public ActionResult Search(string searchValue)
You can use an Ajax call like that : 您可以像这样使用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');
}
}
})
})
})
And Your Method in controller : 和控制器中的方法:
[HttpPost]
public ActionResult asd(string searchValue)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.