0

我在 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 });
    }

我觉得控制器没问题。我认为的问题是视图。有任何想法吗?谢谢!

4

1 回答 1

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() 然后为我检查它的工作。

于 2013-10-22T09:21:54.567 回答