Html.Beginform没有在我的控制器中触发

时间:2013-10-22 07:51:02

标签: c# asp.net-mvc forms

我一直在MVC中使用Html.Beginform一段时间,但现在我遇到了一个奇怪的问题。我的表单上有3个提交按钮,可以在Html.Beginform中触发操作。问题是,它没有解雇。我不知道这是我观点的结构还是我错过了什么。

以下是我的观点:

<div id="maincontent" class="row select_sales"><!--CONTENT START-->
    <div id="page-title" class="text-blue col-md-12"><h2>Create DocPack : @Model.DocumentPackageNumber</h2></div>
    <div class="col-md-12">
      <ol class="breadcrumb">
        <li class="item1" style="color:#999999">Select Product</li>
        <li class="item2" style="color:#49afcd">Assign Manager and Planner</li>
        <li class="item3" style="color:#49afcd">Confirm DocPack</li>
      </ol>
    </div>
    <div id="dv-body-content" class="col-md-12"><br />
    @using (Html.BeginForm("CreateDocumentPackage", "home", FormMethod.Post))
    {
    <div id="select_product">
    <form class="form-horizontal col-md-7 col-md-offset-3" role="form">
      <div class="text-center">
         <div class="form-group">
            <label for="exampleInputEmail1">Select Product</label>
            <select class="form-control" style="margin:0 auto; width:300px !important;">
              <option>Standard Life</option>
            </select>
          </div>
           <div>
            <a href="~/home/index" class="btn btn-default">&nbsp;&nbsp;&nbsp;Back&nbsp;&nbsp;&nbsp;</a>
            <a href="#" class="btn btn-primary" onclick="confirm1()">&nbsp;&nbsp;&nbsp;Confirm&nbsp;&nbsp;&nbsp;</a>
          </div>
      </div>
     </form>
     </div>

    <div id="select_sales" style="display:none">
          <form class="form-horizontal col-md-6 col-md-offset-3" role="form">
            <div class="form-group">
              <label for="exampleInputEmail1">Select Sales Manager (PRC)</label>
              @Html.DropDownListFor(model => model.SalesManager, 
              new SelectList(
              new List<Object>{ 
                   new { value = "John Doe" , text = "John Doe"  },
                   new { value = "Cambridge Wong"  , text = "Cambridge Wong"  },
                   new { value = "Will Smith"  , text = "Will Smith"  },
                   new { value = "Bryan Adams"  , text = "Bryan Adams"  },
                }, "value", "text", Model.SalesManager), new { @class="form-control"})
            </div><br />
            <div class="form-group">
              <label for="exampleInputEmail1">AMG Financial Planner</label>
              @Html.DropDownListFor(model => model.AmgFinancialPlanner,
              new SelectList(
              new List<Object>{ 
                   new { value = "Sydney White" , text = "Sydney White"  },
                   new { value = "Bill Armstrong"  , text = "Bill Armstrong"  },
                   new { value = "Karen Michaels"  , text = "Karen Michaels"  }
                }, "value", "text", Model.AmgFinancialPlanner), new { @class="form-control"})
            </div>
            <div class="text-center">
                <a href="#" class="btn btn-default" onclick="back1()">&nbsp;&nbsp;&nbsp;Back&nbsp;&nbsp;&nbsp;</a>
                <a href="#" class="btn btn-primary" onclick="confirm2()">&nbsp;&nbsp;&nbsp;Confirm&nbsp;&nbsp;&nbsp;</a>
            </div>
          </form>
     </div>

    <div id="existing_dockpack" style="display:none">
      <div class="text-center">
        <label>Do you have an existing Dockpack?</label>
        <p>Would you like to copy it?</p>
      </div>
      <div class="text-center">
        <a data-toggle="modal" href="#" class="btn btn-default" onclick="back2()">&nbsp;&nbsp;&nbsp;Back&nbsp;&nbsp;&nbsp;</a>
        <a data-toggle="modal" href="#existing-docpack-no" class="btn btn-default">&nbsp;&nbsp;&nbsp;No&nbsp;&nbsp;&nbsp;</a>
        <a data-toggle="modal" href="#existing-docpack-yes" class="btn btn-primary">&nbsp;&nbsp;&nbsp;Yes&nbsp;&nbsp;&nbsp;</a>
      </div>
    </div>  
    <div class="modal fade" id="existing-docpack-yes" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-body">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><br />
            <h4 class="modal-title">Input existing Docpack Number</h4><br />
            @Html.TextBoxFor(model => model.PrevDocumentPackageNumber, null, new { @class="form-control", @placeholder="DocPack number"})<br /><br />
            <a href="#" type="button" class="btn btn-default" data-dismiss="modal">Cancel</a>
             <input type="submit" class="btn btn-primary" value="Confirm" />

          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    <div class="modal fade" id="existing-docpack-no" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-body">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><br />
            <h4 class="modal-title">Single individual?</h4><br /><br />
            <a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>
            <input type="submit" class="btn btn-danger" value="No" />
            <input type="submit" class="btn btn-primary" value="Yes" />
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal --> 
    }

   </div><!--CONTENT END-->

不介意div。我把Html.Beginform放在我需要渲染的字段之前。我有3个提交按钮,根据按钮,数据会有一些变化。但是后来,主要问题是该行动没有解雇。我试过将它放在顶部,闭合支架似乎丢失了。

这是我的家庭控制器。

    [HttpPost]
    [InPrivate]
    public ActionResult CreateDocumentPackage(DocumentPackageDetailsViewModel viewModel)
    {
        var documentPackage = new DocumentPackage() { 
            DocumentPackageNumber = viewModel.DocumentPackageNumber, 
            UserId = WebUser.CurrentUser.Id, Title = viewModel.DocumentPackageNumber, 
            DocumentPackageStatus = DocumentPackageStatus.Open, 
            Creator = WebUser.CurrentUser.Firstname + " " + WebUser.CurrentUser.Lastname, 
            IsSingleIndividual = viewModel.IsSingleIndividual,
            SalesManager = viewModel.SalesManager ?? "",
            AmgFinancialPlanner = viewModel.AmgFinancialPlanner ?? "",
            PrevDocumentPackageNumber = viewModel.PrevDocumentPackageNumber
        };
        _documentPackageRepository.Add(documentPackage);
        _documentPackageRepository.Save();

        _documentRepository.CreateDocument(1, documentPackage.Id, Convert.ToInt16(documentPackage.ClientId), WebUser.CurrentUser.Id);
        _documentRepository.CreateDocument(2, documentPackage.Id, Convert.ToInt16(documentPackage.ClientId), WebUser.CurrentUser.Id);
        _documentRepository.CreateDocument(3, documentPackage.Id, Convert.ToInt16(documentPackage.ClientId), WebUser.CurrentUser.Id);
        _documentRepository.CreateDocument(4, documentPackage.Id, Convert.ToInt16(documentPackage.ClientId), WebUser.CurrentUser.Id);

        return RedirectToAction("show", new { id = documentPackage.Id });
    }

我认为控制器很好。我认为问题是View。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:4)

我检查了你的html,它在Html.BeginForm()中包含两个表单标签

@using (Html.BeginForm("CreateDocumentPackage", "home", FormMethod.Post))
    {
    <div id="select_product">
    <form class="form-horizontal col-md-7 col-md-offset-3" role="form">

<div id="select_sales" style="display:none">
    <form class="form-horizontal col-md-6 col-md-offset-3" role="form">

删除你的表单标签,只使用Html.BeginForm()然后检查它的作品。