2013-08-20 31 views
2

我有一個接觸形式的局部視圖。後從局部視圖更改URL

我的問題是,形式發佈後,控制器被重定向到局部視圖的實際URL(「本地主機:/Views/ContactUs/MoreInformationRequest.cshtml」)

我想保留相同的URL並僅顯示ViewData [「MsgSent」]消息。

這是調用局部視圖:

@Html.Partial("~/Views/ContactUs/MoreInformationRequest.cshtml") 

觀:

@using (Html.BeginForm("MoreInformationRequest","ContactUs")) 
    { 
     ..... 

     <input type="submit" value="send" /><br /> 
     @ViewData["MsgSent"] 

    } 

控制器:

[HttpPost] 
    public ActionResult MoreInformationRequest(ContactUs contacts) 
    { 

     ..... 

     ViewData["MsgSent"] = "Message sent!" 
     return View(); 

    } 
+0

你用Jquery的ajax很好嗎? – WannaCSharp

回答

0

在我這個解決方案就出來了初 - 重定向到同一頁面上,使用:

Request.Redirect("~/"); 

但我不喜歡這個解決方案,所以我用客戶端代碼,將數據發送給解決了這個控制器:

<script> 
        var request; 
        $('#formMoreInformationRequest').submit(function() { 

         var Name = document.getElementById('Name'); 
         var Phone = document.getElementById('Phone'); 

         // without this the validations in the page are not working. 
         if (Name.value == "") {        
          return false;} 

         else if (Phone.value == "") {       
          return false; } 

         else { 

          $('#overlay').show() 


          if (request) { 
           request.abort(); 
          } 

          // setup some local variables 
          var $form = $(this); 

          // let's select and cache all the fields 
          var $inputs = $form.find("input, select, button, textarea"); 

          // serialize the data in the form 
          var serializedData = $form.serialize(); 


          // let's disable the inputs for the duration of the ajax request 
          $inputs.prop("disabled", true); 


          // fire off the request to /form.php 
          request = $.ajax({ 
           url: "/ContactUs/MoreInformationRequest", 
           type: "post", 
           data: serializedData 
          }); 


          // callback handler that will be called on success 
          request.done(function (response, textStatus, jqXHR) { 
           $('#overlay').hide() 
           $("#moreInfoMsg").html("Message Sent!"); 
          }); 

          // callback handler that will be called on failure 
          request.fail(function (jqXHR, textStatus, errorThrown) { 
           $('#overlay').hide() 

           // log the error to the console 
           alert(
            "The following error occured: " + 
            textStatus, errorThrown 
           ); 
          }); 

          // callback handler that will be called regardless 
          // if the request failed or succeeded 
          request.always(function() { 
           // reenable the inputs 
           $inputs.prop("disabled", false); 
          }); 


          // prevent default posting of form  
          event.preventDefault(); 

         } 
</script> 
1

你可以使用重定向重新顯示加載的局部視圖頁面:

return RedirectToAction("OriginalAction"); 

你也可以返回一個特定的觀點,特別是從原來的作用的看法:

return View("OriginalView"); 
+0

基本上它工作正常,但URL仍然發生變化,並且頁面中的其他元素無法正確加載。不管怎麼說,還是要謝謝你。 – Eyal

1

後到服務器使用jQuery並從javascript函數返回false來停止默認處理(即,從控制器發送到新的URL。

+0

是的,這正是我所做的。謝謝! – Eyal