重定向到动作不正确工作在我的控制器在ASP.净MVC

本文关键字:我的 控制器 MVC ASP 工作 不正确 重定向 | 更新日期: 2023-09-27 18:14:57

我不确定我这里是否做错了什么。

我有一个。get()在javascript调用我的控制器和在控制器的动作我想重定向到另一个动作。问题是重定向进入"事件",但不加载页面/视图,我得到一个错误从get调用回来。我遵循杰森·邦廷在这里的SO线程的建议。get的错误是

没有找到视图'JumbotronSearch'或其母视图,或者没有视图引擎支持搜索的位置。以下位置被搜索:
~/Views/Home/JumbotronSearch.aspx
~/Views/Shared/JumbotronSearch.aspx
~/Views/Home/JumbotronSearch.cshtml
~/Views/Home/JumbotronSearch.cshtml
~/Views/Home/JumbotronSearch.cshtml
~/Views/Shared/JumbotronSearch.cshtml
~/Views/Shared/JumbotronSearch.cshtml
~/Views/Shared/JumbotronSearch.vbhtml
~/Views/Shared/JumbotronSearch.vbhtml
~/Views/Shared/JumbotronSearch.vbhtml

javascript

$.get(scope.enumControllers.jumbotronSearch, model, function(result) {
  if (result.error == true) {
    yb.base.displayNotification(result.message, 'danger');
  }
}).fail(function(jqXHR, textStatus) {
  yb.base.displayNotification("Oh no! Something went wrong sending your request. Please contact the  help desk.", 'danger');
});

这是我的控制器代码

    [AcceptVerbs(HttpVerbs.Get)]
    [AllowAnonymous]
    public ActionResult JumbotronSearch(SearchCriteria searchCriteria)
    {
        return RedirectToAction("Events", new { searchCriteria = searchCriteria });
    }
    [AcceptVerbs(HttpVerbs.Get)]
    [AllowAnonymous]
    public ActionResult Events(SearchCriteria searchCriteria)
    {
        try
        {
            var viewModel = new EventsViewModel();
            //do some work here
            return View(viewModel);
        }
        catch (Exception ex)
        {
            // log exception in file or db or both
            return Json(new { error = "true", message = "Oh No! Something happened trying to submit your search. Please contact Help Desk." });
        }
    }

重定向到动作不正确工作在我的控制器在ASP.净MVC

这可能是因为你的动作方法是POST,看看这里的答案如何使用POST谓词重定向到页面?

您可以将Events方法中的动作动词更改为

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

或者您可以将action方法调用更改为this

[HttpPost]
[AllowAnonymous]
public void JumbotronSearch(SearchCriteria searchCriteria)
{
    Return Events (searchCriteria);
}

你不能重定向一个帖子,因此你的调用

$.post

将在JumbotronSearch结束,但不会重定向到Events

HTTP GET代替

$.get(scope.enumControllers.jumbotronSearch, model, function(result) {

[HttpGet]
[AllowAnonymous]
public void JumbotronSearch(SearchCriteria searchCriteria)
{
    RedirectToAction("Events");
}

看这篇文章