2016-11-18 67 views
0

我有一個帶有搜索框的表單。當某人在搜索框中鍵入內容並點擊搜索按鈕時,我正在嘗試執行一篇文章來捕獲搜索過濾器,然後觸發一個視圖。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> 
+0

爲什麼你會需要一個'POST'?你沒有做任何插入或更新,只是基於參數('searchfilter')獲取結果 - 使用'GET'。 – trashr0x

+0

基於我在網上看到的一些東西,因爲當頁面呈現時searchfilter不在那裏,所以我需要做一個帖子來獲取使用的值。即使我能夠使用get雖然這仍然似乎是一個問題,我試圖傳遞一個參數,因爲郵件正確觸發時沒有指定 –

+0

作爲一方,您通常不應該從Post操作返回視圖。Google Post重定向獲取 –

回答

0

與大多數其他的答案指出,這需要的協議是一個HttpGet請求,而不是一個HttpPost請求,我認爲,這可能是通過更改您的HTML解決。

HTML:

@using (Html.BeginForm("SearchSpots", "Spots", FormMethod.Get, null)) 
{ 
    <input id="searchfilter" type="text" class="form-control" autocomplete="off" placeholder="Search" name="searchfilter"> 
    <input class="btn btn-default" type="submit" <i class="glyphicon glyphicon-search"</i>/>  // part that needs changing 
} 

控制器:

[HttpGet] 
public ActionResult SearchSpots(string searchfilter) 
{ 
    // logic  
} 

我相信你的問題可以與this<button>正是它是..按鈕..它基本上什麼都不做,主要用於JS目的。但是,<input type="submit" />其實提交的周圍形式。

我希望這有助於!

UPDATE

我確實需要輸入來傳遞參數。我仍然有它被過去之後同樣的錯誤,我不得不做出讓它運行

[HttpGet] 
    [AllowAnonymous] 
    public ActionResult SearchSpots(string searchfilter) 
    { 
     return RedirectToAction("Index", new { filter = searchfilter}); 
    } 

這最後的調整,我需要重定向,而不是試圖返回一個視圖

+0

我錯過了我的答案中的元素類型(無恥的複製粘貼),將這個因素考慮在內。 – trashr0x

+0

@ trashr0x所以你的意思是將'

+0

不,我說我已經將你的建議添加到了我的答案中(儘管我不確定這是否是問題)。 – trashr0x

0

的觀點,我相信你在

@using (Html.BeginForm("SearchSpots", "Spots", FormMethod.Post)) {...

失蹤 FormMethod.Post
+0

我確實嘗試添加這個,但沒有運氣。我仍然得到相同的錯誤,這似乎是它窒息的參數,我期望在控制器功能 –

0

你不需要兩種形式在你的HT ML和這個代碼工作,後搜索文本

<!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> 
    <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", FormMethod.Post)) 
       { 
        @Html.AntiForgeryToken() 
        <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> 

而且你可以在你的操作添加[ValidateAntiForgeryToken]

+0

我做了這些更改,但仍然得到相同的錯誤,因爲基本崩潰,如果我要求在我的控制器行動中的參數 –

+0

@DaveWade你能分享你的get方法來顯示視圖嗎? –

+0

我分享了搜索獲取視圖和上方視圖的呼叫 –

1

做一個GET,而不是一個POST - 你又沒什麼做任何插入或更新,只是基於參數獲取結果(searchfilter)。使用GET時,表單中輸入元素的值將作爲參數附加到目標URL的查詢字符串,這會產生類似mywebsite.com/spots/spotsearch?searchfilter=whateverValueInTheInputBox(取決於您如何配置路由)。

剃刀:

@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>  
} 

控制器:

public class SpotsController : Controller 
{ 
    [HttpGet] 
    [AllowAnonymous] 
    public ActionResult SearchSpots(string searchfilter) 
    { 
     // ...   
    } 
} 

編輯:按照@BviLLe_Kid,你可以嘗試用<input>更換<button>

編輯2:不禁想知道爲什麼你要通過SearchSpots代理Index的調用,導致不必要的重定向。如果SearchSpots所做的全部重定向到Index,爲什麼不直接將表格提交到Index

剃刀:

@using (Html.BeginForm("Index", "Spots", FormMethod.Get, null)) 
{ 
    <!-- remember to rename to name="filter" below --> 
    <input id="filter" type="text" class="form-control" autocomplete="off" placeholder="Search" name="filter"> 
    <input class="btn btn-default" type="submit" <i class="glyphicon glyphicon-search"</i>/> 
} 

控制器:

// 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()); 
    } 

} 
+0

沒有得到的運氣,雖然我認爲你是對的,我應該做一個GET而不是POST。在我做了更改並崩潰後,我從控制器操作中刪除了參數,並且它很好,因此它似乎與我期望模型綁定器爲我提供的參數有關。 –

+0

爲什麼'返回索引(searchfilter);'?如果你要返回一個特定行爲的視圖,只需'返回View(yourModelHere);'然後在你的視圖中添加'@model modelType'(如果'yourModelHere'是一個字符串,例如你可以寫'@model string')。 – trashr0x

+0

基本上索引是我希望它通常打開的視圖,並顯示一個未經過濾的列表,您可以使用文本框進行過濾。我試圖調用這個視圖,這個視圖已經有了我正在顯示的數據的模型,我只是想把一個初始過濾器放在數據上。我會發布呼叫並查看 –