2014-09-29 44 views
0

我使用的引導模態來創建數據庫中一個新的項目。ASP.NET MVC莫代爾BeginForm發送空HtmlAttribute

我有一個包含以下代碼的頁面。頁面網址是.../ProjectManager/Projects/1。

我想通過這個值ID(在URL)到我的模式,然後我需要在表單中使用它。當我運行此代碼時,控制器方法參數id將變爲null。

<a data-toggle="modal" class="btn btn-info" href="AddModule" data-target="#NewModule">New Module</a> 

    <div class="modal fade" id="NewModule" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
     <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
        <h4 class="modal-title text-left">Add New Module/Submodule</h4> 
       </div> 
       @using (Html.BeginForm("AddModule", "ProjectManager", FormMethod.Post, new { id = ViewContext.RouteData.Values["Id"].ToString() })) 
       { 
        <div class="modal-body"> 
         <div class="te"> 
          <div class="form-horizontal"> 
           @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 


           <div class="form-group"> 
            <div class="col-md-4"> 
             @Html.Label("Module Name", htmlAttributes: new { @class = "control-label col-md-2" }) 
            </div> 
            <div class="col-md-8"> 
             @Html.Editor("ModuleName", new { htmlAttributes = new { @class = "form-control" } }) 
            </div> 
           </div> 
          </div> 
         </div> 
        </div> 
        <div class="modal-footer"> 
         <a href="#" class="btn" data-dismiss="modal">Close</a> 
         <button type="submit" class="btn btn-primary">Create Module</button> 
        </div> 

       } 
      </div> 
     </div> 
    </div> 

這是我的控制器方法

[HttpPost] 
    public ActionResult AddModule(string ModuleName, string id) 
    { 

     return RedirectToAction("Projects"); 
    } 
+0

是的,我已經檢查並沒有id參數發送到該頁面。這是我所要求的。謝謝。 – Halislus 2014-09-29 11:08:16

回答

0

您正在使用的BeginForm錯誤的過載。您正在使用BeginForm(HtmlHelper, string, string, FormMethod, object)。您可以在MSDN上看到該對象是htmlAttributes,因此它設置了表單元素的id。您需要使用BeginForm(HtmlHelper, string, string, object, FormMethod)。在這個超載的對象是routeValues,這是你想要的。

所以你需要全面交換參數:

@using (Html.BeginForm("AddModule", "ProjectManager", 
    new { id = ViewContext.RouteData.Values["Id"].ToString() }, 
    FormMethod.Post)) 
+0

你是完美的。它解決了這個問題。非常感謝。 – Halislus 2014-09-29 11:18:15