2017-04-10 102 views
0

我目前正在插入MVC 5應用程序的記錄功能。出於某種原因,後操作方法不會觸發。我在View上創建了一個按鈕,該按鈕應該觸發後操作方法。不知道爲什麼它沒有射擊。表單元素在局部視圖中,按鈕控件不是承載局部視圖的主視圖。MVC Post操作方法沒有觸發

下面是代碼

主視圖

@model CC.GRP.MCRequest.ViewModels.NewRequestViewModel 


<div class="newrequest"> 
    @Html.Partial("~/Views/Request/RequestHeaderView.cshtml"); 
</div> 


<div id="buttonalign" class="buttonalign" > 
    <button type="button" class="btn btn-primary">Submit</button> 
    <button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Request_Insert", "Request")'" >Save as draft</button> 

</div> 

請求報頭查看

@model CC.GRP.MCRequest.ViewModels.NewRequestViewModel 
    @using (Html.BeginForm()) 
    { 

    <div id="NewRequest"> 
     <div class="row"> 
      <div class="col-md-4"> 
       <div class="form-group"> 
        @Html.LabelFor(model => model.RequestID, htmlAttributes: new { @class = "control-label col-md-4" }) 
        <div class="col-md-6"> 
         <div class="editor-field"> 
          @Html.EditorFor(model => model.RequestID, new { htmlAttributes = new { @class = "form-control", style = "width:100%", @readonly = "readonly" } }) 

         </div> 
         @Html.ValidationMessageFor(model => model.RequestID, "", new { @class = "text-danger" }) 
        </div> 
       </div> 
       <div class="clearfix"></div> 
       <div class="form-group"> 

        @Html.LabelFor(model => model.Name1, htmlAttributes: new { @class = "control-label col-md-4" }) 

        <div class="col-md-6"> 
         <div class="editor-field"> 
          @(Html.Kendo().ComboBoxFor(model => model.Name1) 

       .HtmlAttributes(new { style = "width:100%" }) 
       .DataTextField("Name1") 
       .DataValueField("CustomerMasterDataId") 

       .DataSource(dataSource => dataSource 
       .Read(read => read.Action("RequestHeader_CustomerData", "Request").Type(HttpVerbs.Post)) 
       ) 
        .Events(e => 
        { 
         e.Change("onCustomerComboChange"); 
        }) 
          ) 
         </div> 
         @Html.ValidationMessageFor(model => model.CustomerNumber, "", new { @class = "text-danger" }) 
        </div> 
       </div> 
       <div class="clearfix"></div> 
       <div class="form-group"> 
        @Html.LabelFor(model => model.CustomerNumber, htmlAttributes: new { @class = "control-label col-md-4" }) 
        <div class="col-md-6"> 
         <div class="editor-field"> 
          @Html.EditorFor(model => model.CustomerNumber, new { htmlAttributes = new { @class = "form-control", style = "width:100%", @readonly = "readonly" } }) 

         </div> 
         @Html.ValidationMessageFor(model => model.CustomerNumber, "", new { @class = "text-danger" }) 
        </div> 
       </div> 
      </div> 
     </div> 

請求控制器

[HttpPost] 
     public ActionResult Request_Insert(NewRequestViewModel userVM) 
     { 
      NewRequestViewModel newReqeustViewModel = new NewRequestViewModel(); 
      if (!ModelState.IsValid) 
      { 
       return null; 
      } 
      requestRepository.CreateRequest(Mapper.Map<Request>(userVM)); 
      //return Json(requestRepository.CreateRequest(userVM)); 
      return View("NewRequestView", newReqeustViewModel); 
     } 
+1

您沒有提交按鈕,並且您_do_擁有的按鈕不是表格的一部分。你知道表單如何工作嗎? –

+0

我改變了按鈕來輸入提交。並在表單元素中包含主視圖的內容。它仍然在努力 – Tom

+1

你能更新你的代碼來反映你現在做出的改變嗎?目前這裏的東西肯定不會起作用,因爲Tieson T.說您的提交按鈕沒有包含在您的表單中。 – sovemp

回答

2

你應該把

<div id="buttonalign" class="buttonalign" > 
    <button type="button" class="btn btn-primary">Submit</button> 
    <button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Request_Insert", "Request")'" >Save as draft</button> 

</div> 

@using (Html.BeginForm("Request_Insert", "Request", FormMethod.Post)) 
    { 
} 

請求頭查看

和 更新

<button type="button" class="btn btn-primary">Submit</button> 

<button type="submit" class="btn btn-primary">Submit</button> 
+0

我試着你說的,我發現沒有區別。 – Tom

+0

@Tom將Html.BeginForm() 更改爲Html.BeginForm(「Request_Insert」,「Request」,FormMethod.Post) –

+0

很奇怪。我改變了開始形式。當我點擊按鈕時,我可以看到一個回傳發生,但它仍然沒有擊中控制器方法。 – Tom