如何重定向到mvc控制器上的另一个动作?

时间:2014-03-04 05:28:01

标签: jquery asp.net-mvc asp.net-mvc-4

我有一个asp.net mvc 4应用程序具有以下索引操作:

public ActionResult Index()
{
    if (User.IsInRole("dealer"))
    {
        return View(db.Commissions.Where(c => c.Dealer.Name == User.Identity.Name));
    }
    else
    {//admin
        return View(db.Commissions);
    }
}

我想将我的页面重定向到另一个名为filter的mvc操作,但是无法使用此代码完成:

   $(function () {
        jQuery.noConflict()
        $("#datefrom").datepicker();
        $("#dateto").datepicker();

        $("#btnfilter").click(function () {
            $.ajax({
                type: "get",
                url: "/commissions/filter/",
                data:{'from':$("#datefrom").val(),to:$("#dateto").val()}
                ,success: function(result){
                    if(result.Success){
                        window.location = "/Filter/Commissions" + $("#datefrom").val() + $("#dateto").val();
                    }
                }
            });
        });

    });

这是过滤器操作:

 public ActionResult Filter(DateTime from, DateTime to)
        {
            if (User.IsInRole("dealer"))
            {
                return View(db.Commissions.Where(c => c.Dealer.Name == User.Identity.Name && c.CreatedDate>=from && c.CreatedDate <=to));
            }
            else

            {//admin
                var test = db.Commissions.Where(c => c.CreatedDate >= from && c.CreatedDate <= to);
                return View(db.Commissions.Where(c=>c.CreatedDate >= from && c.CreatedDate <= to));
            }
        }

在服务器上点击过滤器操作但是当我查看小提琴手时出现以下错误:

[InvalidOperationException: The view 'filter' or its master was not found or no view engine supports the searched locations. The following locations were 

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请尝试这样做,但我在您的代码中有一个疑问,即您在过滤操作中返回true/false

$(function () {
    jQuery.noConflict()
    $("#datefrom").datepicker();
    $("#dateto").datepicker();

    $("#btnfilter").click(function () {
        $.ajax({
            type: "get",
            url: '@Url.Action("filter","commissions")',
            data:{'from':$("#datefrom").val(),to:$("#dateto").val()},
            success: function(result){
                if(result.Success){
                    window.location.href = '@Url.Action("filter","commissions",new{from=$("#datefrom").val(),to=$("#dateto").val()})';
                }
            }
        });
    });

});