[英]MVC 5 Html.BeginForm without model
我在_Layout頁面上有一個簡單的搜索表單。
如何輕松地將search-fld中的值傳遞給控制器? 無需創建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>
}
如果我運行這個,那么“search-fld”會被發送到控制器(offcourse)
使用Ajax表單並使用Jquery獲取值?
只需為input
提供name
屬性:
<input type="text" placeholder="Search" id="search-fld" name="searchValue">
然后將該名稱與控制器HttpPost
方法中的參數進行匹配:
[HttpPost]
public ActionResult Search(string searchValue)
您可以像這樣使用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)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.