2014-09-29 43 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