我有一個帶有搜索框的表單。當某人在搜索框中鍵入內容並點擊搜索按鈕時,我正在嘗試執行一篇文章來捕獲搜索過濾器,然後觸發一個視圖。ASP.NET MVC BeginForm無法發佈參數
這裏是控制器代碼
public class SpotsController : Controller
{
[HttpPost]
[AllowAnonymous]
public ActionResult SearchSpots(string searchfilter)
{
//your code here....
return Index(searchfilter);
}
這裏是我的看法了代碼,直到那就是tryign做部分提交
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Haunt Spotter</title>
</head>
<form id="__AjaxAntiForgeryForm" action="#" method="post"><@Html.AntiForgeryToken()></form>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
@using (Html.BeginForm("SearchSpots", "Spots"))
{
<input id="searchfilter" type="text" class="form-control" autocomplete="off" placeholder="Search" name="searchfilter">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
}
</div>
</div>
</div>
如果我把參數關閉控制器的功能它工作正常。如果不是,它似乎崩潰,並嘗試重新顯示一個失敗的get,因爲我只有一個post函數。我猜測我有一些與參數搞砸了,但我無法弄清楚它是什麼。任何幫助將不勝感激。
UPDATE
意見的基礎上我已經改變了我後到獲得
[HttpGet]
[AllowAnonymous]
public ActionResult SearchSpots(string searchfilter)
{
//your code here....
return Index(searchfilter);
}
和我的視圖代碼這個
@using (Html.BeginForm("SearchSpots", "Spots", FormMethod.Get, null))
{
<input id="searchfilter" type="text" class="form-control" autocomplete="off" placeholder="Search" name="searchfilter">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
}
不幸的是我仍然有原來的問題。如果我從我的控制器調用中移除searchfileter參數,那麼它會毫無問題地進入調用,但是當我期望模型綁定器給我一個searchfilter時,它會崩潰。
這裏是我在我的搜索功能
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Spots
public ActionResult Index(string filter = "")
{
ViewBag.initialFilter = filter;
if (User.IsInRole("SiteAdmin"))
{
return View(db.Spots.ToList());
}
else
{
return View(db.Spots.Where(x => x.Approved).ToList());
}
}
重定向到呼叫和顯示
@model IEnumerable<HauntSpots.Models.Spot>
@{
ViewBag.Title = "Index";
}
<h2 class="align-right">Haunt Spots</h2>
@if (Context.User.IsInRole("SiteAdmin"))
{
<p style="padding-top:20px">
<a href="@Url.Action("Create")" title="Add New Spot" class="btn btn-primary"><i class="icon-plus-sign"></i> Add New</a>
</p>
}
<table id="dt-spots" class="table table-striped">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
@if (Context.User.IsInRole("SiteAdmin"))
{
<th></th>
}
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@if (Context.User.IsInRole("SiteAdmin"))
{
@Html.Hidden(Url.Action("Edit", "Spots", new { id = item.Id }))
<a style="color: Red; vertical-align: middle; font-size: 2em" href="@Url.Action("Delete", "Spots", new { id = item.Id })" title="Delete Spot" class="btn"><i class="icon-remove-sign"></i></a>
}
else
{
@Html.Hidden(Url.Action("Details", "Spots", new { id = item.Id }))
}
</td>
<td>
@if (item.Image == null)
{
<img width="100" height="100"
src="~/Content/Images/NoPhoto.jpg" class="img-rounded" />
}
else
{
<img width="100" height="100"
src="@item.Image" class="img-rounded"/>
}
</td>
<td >
<div class="form-group pull-left col-md-2">
<h4>@item.Title </h4>
<h5 style="clear: left">
@if (item.Address != null)
{
<span>@item.Address</span>
<br/>
}
@if (item.State == null)
{
<span>@item.City</span><br/>
<span>@item.Country</span>
}
else
{
if (item.State == "")
{
<span>@item.City</span>
<br/>
<span>@item.Country</span>
}
else
{
<span>@item.City, @item.State</span>
<br/>
<span>@item.Country</span>
}
}
</h5>
</div>
<div class="form-group pull-left col-md-8">
<h6>@item.Summary</h6>
</div>
</td>
@if (Context.User.IsInRole("SiteAdmin"))
{
<td>
@if (@item.Approved)
{
<span style="color: green">Approved</span>
}
else
{
<span style="color: red">Not Approved</span>
}
</td>
}
</tr>
}
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
//Initalize and configure DataTables
$('#dt-spots').dataTable({
"oSearch": { "sSearch": "@ViewBag.initialFilter" }
});
$("tbody").on("click", "tr", function() {
window.location = $(this).find('input').attr('name');
});
});
</script>
爲什麼你會需要一個'POST'?你沒有做任何插入或更新,只是基於參數('searchfilter')獲取結果 - 使用'GET'。 – trashr0x
基於我在網上看到的一些東西,因爲當頁面呈現時searchfilter不在那裏,所以我需要做一個帖子來獲取使用的值。即使我能夠使用get雖然這仍然似乎是一個問題,我試圖傳遞一個參數,因爲郵件正確觸發時沒有指定 –
作爲一方,您通常不應該從Post操作返回視圖。Google Post重定向獲取 –