繁体   English   中英

使用ajax后,返回同一控制器的另一个MVC3视图

[英]return another MVC3 view of the same controller after ajax is used

如何返回同一控制器的另一个视图? 我为应用程序进行了一些调试:ajax方法调用控制器动作,传递参数,执行指令。

return View("GetEmployeeDays", model);,

GetEmployeeDays ”视图从模型接收值并进行填充,但是最终在浏览器中我收到了初始视图(来自我发出的请求)-而不是GetEmployeeDays视图

来自Global.asax的路由代码:

public static void RegisterRoutes(RouteCollection routes)
 {
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );          
  }

调用动作控制器并传递参数的JQuery代码:

<script type="text/javascript">
  $(document).ready(function () {
  $('li').click(function () {
  $.ajax({
    url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: { userCode: $(this).attr('id') }
   })
  });
});

应该呈现GetEmployeeDays视图的控制器操作:

   [HttpGet]
   public ActionResult GetEmployeeDays(string userCode)
   {
      .....
    return View("GetEmployeeDays", model);
   }

您无法通过ajax请求进行重定向。 尝试一个简单的GET请求:

<script type="text/javascript">
$('li').on('click', function()
{
   document.location.href = '@Url.Action("GetEmployeeDays", "ApproveWork")' + '?param1=val1&param2=val2';
});
</script>

控制器现在很好。

编辑:按要求更新

我不知道您要重新渲染的原始视图是什么,我想您的问题很可能是您应该以PartialView的形式返回此视图,因为您正在发出Ajax请求。 另外,看起来您实际上并没有在任何地方渲染结果。 您对提琴手的反应是什么? 它显示返回的HTML吗? 如果是这样,您可能只需要利用$ .ajax中的.done回调将其转储到页面上。

<script type="text/javascript">
$(document).ready(function () {
$('li').click(function () {
$.ajax({
    url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
    type: 'GET',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: { userCode: $(this).attr('id') }
    }).done(function() {
       $(this).addClass("done");
  });
});
</script>

如果只想将某些内容加载到页面的一部分中而不重新加载到新页面,则应在ajax函数的回调中更新页面的特定部分。

$(function(){
 $('li').click(function () {
     var uCode=$(this).attr("id");
     $.get("@Url.Action("GetEmployeeDays","ApproveWork")?userCode="+uCode,
                                                          function(response){
       $("#DivToBeUpdated").html(response);
     });
 });
});

如果要重定向到新视图,则应通过更新location.href属性值来实现。

$(function(){
 $('li').click(function () {
     var uCode=$(this).attr("id");
     var url="@Url.Action("GetEmployeeDays","ApproveWork")?userCode="+uCode;
     window.location.href=url;         
     });
 });
});

从@David Diez构建,您可以将参数附加到您的请求中,如下所示:

<script type="text/javascript">
$('li').on('click', function()
{
   document.location.href = '@Url.Action("GetEmployeeDays", "ApproveWork", new { caseSensativeParamName = "foo" })';
});
</script>

当然,您也可以采用JavaScript路线。 这样的事情应该起作用:

<script type="text/javascript">
$('li').on('click', function()
{
   var targetUrl = '@Url.Action("GetEmployeeDays", "ApproveWork")';
   var yourParam = $("#input-id").val();
   targetUrl = targetUrl + '?yourServerParamName=' + yourParam;
   document.location.href = targetUrl;
});
</script>

干杯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM