2014-10-30 150 views
0

我鑑於ASP.NET MVC獲取ID參數控制器

using (Html.BeginForm("Test", "AcceptStatement", "Account", FormMethod.Post, new { id = Model.StatementID })) 

在控制器使用方法:在控制

public ActionResult AcceptStatement(int id) 

id參數最終成爲一個null值。我怎樣才能得到id參數值到我的控制器與Html.BeginForm

+0

我沒有以您使用它的方式看到該版本的BeginForm。確保您正確調用BeginForm。 – 2014-10-30 16:28:54

回答

0

您使用的BeginForm錯誤的過載。您正在通過如下:

  • actionName: 「測試」
  • controllerName: 「AcceptStatement」
  • routeValues: 「帳戶」
  • formMethod: FormMethod.Post
  • htmlAttributes:{ id = Model.StatementID }

這顯然是錯誤的,因爲它沒有任何意義。您可能想要:

Html.BeginForm("AcceptStatement", "Account", new { id = Model.StatementID }, FormMethod.Post, null) 

使用this過載。

+0

謝謝,它已經解決了! – trilobit 2014-10-30 16:45:08

0

使用信息中this:

基本上,你的視圖:

@using (Html.BeginForm()){  
     <p> Title: @Html.TextBox("SearchString") <br /> 
     <input type="submit" value="Filter" /></p> 
     } 
</p> 

隨着一個控制器:

public ActionResult Index(string searchString) 
{   
    var movies = from m in db.Movies 
        select m; 

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

    return View(movies); 
} 

的替代方法通過模型的值提供給控制器的方法將使用:

@Html.ActionLink("Details", "Details", new { id=item.BayID }) 

有了您的控制器的方法是這樣的:

public ActionResult Details(int? id) 
{ 
    ... 
+0

這是如何解決問題的? – 2014-10-30 16:31:01

+0

顯示OP如何將輸入/模型值傳遞給控制器​​。很好,我希望。 – jbutler483 2014-10-30 16:33:11

+0

同樣,這與問題(這是不正確的Html.BeginForm的用法)有什麼關係? – 2014-10-30 16:35:58