2014-10-08 107 views
-1

我試圖將C#對象作爲參數傳遞迴控制器。它是這樣做的:MVC4不會將模型傳遞給控制器​​

@model Models.AdvancedSearchTerms 

@{ 
    ViewBag.Title = "Loading"; 
}  


@Html.DisplayFor(Model => Model.Keyword) 

@using (Html.BeginForm("MetaSearch", "AdvancedSearch", new { term = Model }, FormMethod.Post)) 
{ 
    <input id="startGatheringData" type="Submit" name="Convert" /> 
} 

正如你所看到的,我嘗試發送整個模型回控制器,如果按下按鈕。但是當控制器中的方法獲取模型時,它是空的。爲了確保模型在視圖中不是空的,您可以看到我試圖使用@ Html.DisplayFor打印一個變量,並且它可以工作。因此模型是不是在查看空的,但消失時,我把它傳遞給控制器​​:

public ActionResult MetaSearch(AdvancedSearchTerms term) 
    { 

     //Do stuff with the model retrived from the View() 
     return View() 
    } 

任何人有任何想法,爲什麼這個對象爲空?我做了很多,不知道爲什麼這次不工作。我也嘗試過只傳遞一個變量,但這也是空的。

在前進,謝謝!

回答

0

你必須爲這個創建輸入字段的形式裏面,如果你不想向他們展示給用戶使用隱藏字段:

@using (Html.BeginForm("MetaSearch", "AdvancedSearch", FormMethod.Post)) 
{ 
    @Html.HiddenFor(x=>x.SomeProperty) 
    @Html.HiddenFor(x=>x.SomeProperty) 

    <input id="startGatheringData" type="Submit" name="Convert" /> 
} 
+0

啊,我很害怕。我只是希望這是一個更簡單的出路:) 我會在一段時間嘗試一下,然後接受這個答案,如果它的工作!謝謝! – 2014-10-08 08:30:35

+0

是的,它的工作原理,我現在就會使用它。我可能會盡快將此更改爲更高效的方式,但因爲我只需要快速修復!謝謝您的幫助! – 2014-10-08 11:18:53

0

如果你想在控制器中使用來自一個視圖的信息再次,一定要做出行動httppost動作

這樣的:

[HttpPost] 
public ActionResult MetaSearch(AdvancedSearchTerms term) 
{ 
//LOGIC 
} 
+0

謝謝,但它在那裏。我剛剛在提供的代碼中通過某種原因刪除了它 – 2014-10-08 08:31:15

0

它可以做到這一點,如果你的模型只包含簡單的屬性(無集合或複雜的OB項目)

@using (Html.BeginForm("MetaSearch", "AdvancedSearch", Model, FormMethod.Post, null)) 

但是你爲什麼要這樣做。通過在其生成的查詢字符串中發佈大量值,來降低性能。最好只回發模型的唯一ID,然後再從控制器中的數據庫中獲取模型。

編輯

此外,該模型不能包含具有相同名稱作爲路由參數的屬性。例如,如果您已使用url: "{controller}/{action}/{id}",定義路線,則該模型不能包含名爲ID的屬性。

1

發送一個值來控制


我真的只是一直在創造我的形式的「搜索」 /過濾器選項;使用類似:

查看

@using (Html.BeginForm()) 
{ 
    <div> 
     <hr /> 
     Search 
     <hr /> 
     Name: @Html.TextBox("SearchString", null, new { @id = "thisID" })<a style="float:right; margin-top:8px" href="@Url.Action("Index", "ControllerName")"> 
     <br /> 
     <input type="submit" value="Filter" id="subBtn" /> 
     <hr /> 
    </div> 
} 

隨着我的控制器代碼是:

 [Authorize] 
     public ActionResult Index() 
     { 
      return View(db.Traders.ToList()); 
     } 

     [HttpPost] 
     public ActionResult Index(string searchString) 
     { 

      var traders = from m in db.Traders //db is my Database Entities 
         select m; 

      if (!String.IsNullOrEmpty(searchString)) 
      { 
       traders = traders.Where(s => s.Name.Contains(searchString)); 
      } 

      return View(traders); 
     } 

所以我的控制器將獲得該文本框的值作爲方法的一部分。


過濾使用搜索和/或組合框(或兩者的一個或沒有)


我實際使用this site實現一個基本的搜索/過濾系統爲我的項目。它幫助很大。

它使用ViewBag允許您創建一個組合框,然後您可以選擇並返回選定的值到您的[HttpPost]操作方法 - 非常適合您的過濾/搜索需要!

這意味着您不必將完整的模型發送回您的控制器,而是通過一個「搜索術語」,然後您可以在您的控制器方法中使用它,並輕鬆地在您的方法中獲得匹配的模型(類似於發送單值如第一個例子)。然後

你的操作方法可能如下:

public ActionResult Index(string movieGenre, string searchString) 
{ 
    var GenreLst = new List<string>(); 

    var GenreQry = from d in db.Movies 
        orderby d.Genre 
        select d.Genre; 

    GenreLst.AddRange(GenreQry.Distinct()); 
    ViewBag.movieGenre = new SelectList(GenreLst); 

    var movies = from m in db.Movies 
       select m; 

    if (!String.IsNullOrEmpty(searchString)) 
    { 
     movies = movies.Where(s => s.Title.Contains(searchString)); 
    } 

    if (!string.IsNullOrEmpty(movieGenre)) 
    { 
     movies = movies.Where(x => x.Genre == movieGenre); 
    } 

    return View(movies); //returns movies matching results 
} 

發送完整型號爲控制器


在傳遞一個控制器的幫助方面,你可能會發現this tutorial因爲它創建了基本的CRUD操作(「創建」允許您將單個模型傳遞給控制器​​)​​。

在「回傳」行動方法,你需要「綁定」的數據,如:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "LastName, FirstMidName, EnrollmentDate")]Student student) 
{ 
//do stuff with this model 
return redirectToAction("Index"); 
} 

這上面的鏈接進行進一步的解釋。

+0

似乎適用於OPs問題:) – jbutler483 2014-10-08 08:58:50

+0

這與問題有什麼關係? – 2014-10-08 09:07:28

+0

OP正在實施一個搜索工具(無論如何都是從變量名稱開始的)。此外,它顯示/教導OP如何將數據從View傳遞到控制器,無論所有字段是否完整(否則您的模型可能需要更改爲允許NULL值);) – 2014-10-08 09:10:40