2013-11-24 73 views
0

在Ajax發佈請求之後調用Submit Action時。它返回值爲「Bad Request」的錯誤方法。爲什麼它不成功?如何解決這個問題?Http發佈請求失敗後返回部分視圖

Ajax請求:

<p> 
    @using (Html.BeginForm("Submit", "Test")) 
    { 
     <span> 
      Price: @Html.TextBox("Price", string.Empty) 
     </span> 

     <input id="submit" type="submit" value="test" /> 
    } 
</p> 

<div id="testPost"> 

</div> 

     @section Scripts { 
     <script type="text/javascript"> 
      $(function() { 
       $('#submit').on("click", function() { 

        var form = $(this).parent("form"); 

        $.ajax({ 
         type: "POST", 
         url: form.attr('action'), 
         data: form.serialize() 
        }) 
         .success(function (html) { 

          $('#testPost').replaceWith(html); 

          alert("Success"); 
         }) 
         .error(function() { 
          alert("Bad Request"); 
         }); 

        return false; 
       }); 
      }); 
     </script> 
     } 

提交行動:

[HttpPost] 
     public ActionResult Submit(Auction auction) 
     { 
      return PartialView("_CurrentPrice",auction); 
     } 

管窺:

<p class="current-price"> 
    <h2>ACTIVE HERE</h2> 
</p> 
+0

檢查你的預期和發送值,記住'form.serialize()'返回一個字符串 – Sarath

回答

0

請試試這個,讓我們知道這是否是工作..

$.ajax({ 
        type: "POST", 
        url: "/test/submit", 
        data: form.serialize() 
       }) 

注:應該有一個控制命名「的TestController」,並命名爲「提交」的動作,你已經有了......

如果一切正常,放的console.log(form.attr(「行動「))並查看返回值是什麼...

+0

嘗試這個代碼,但它再次失敗。它返回錯誤消息。 – theChampion

0

您應該使用螢火蟲和檢查對象arguments[0]和屬性arguments[0].responseText進入錯誤方法。它有助於瞭解服務器返回錯誤的原因。

或者嘗試 -

.error(function() { 
$.('body').html(arguments[0].responseText); 
} 

而且檢查名稱局部視圖。它必須等於「_CurrentPrice」

1

問題已解決。部分視圖不在共享文件夾中。