2013-08-20 30 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