简体   繁体   English

找不到视图-ASP.NET MVC错误

[英]View not found - asp.net mvc error

I am getting an error saying the view Update has not been found, I wanted to show the TaskDetail view not the update view... 我收到一条错误消息,说未找到视图更新,我想显示TaskDetail视图而不是更新视图...

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Update(int taskid, string status)
    {
        if (!status.Equals("closed", StringComparison.OrdinalIgnoreCase) &&
            !status.Equals("opened", StringComparison.OrdinalIgnoreCase))
            ModelState.AddModelError("", "incorrect status, please try again");

        if (!this.ModelState.IsValid)
            return TaskDetail(taskid);

        if (status.Equals("Closed", StringComparison.OrdinalIgnoreCase))
            _service.CloseTask( taskid, true);
        else
            _service.CloseTask(taskid, false);

        this.FlashInfo("success, task status has been updated...");
        return RedirectToAction("TaskDetail");

} }

Exception: 例外:

$exception{"The view 'Update' or its master was not found. The following locations were searched:\r\n~/Areas/Tasks/Views/TaskManager/Update.aspx\r\n~/Areas/Tasks/Views/TaskManager/Update.ascx\r\n~/Areas/Tasks/Views/Shared/Update.aspx\r\n~/Areas/Tasks/Views/Shared/Update.ascx\r\n~/Views/TaskManager/Update.aspx\r\n~/Views/TaskManager/Update.ascx\r\n~/Views/Shared/Update.aspx\r\n~/Views/Shared/Update.ascx"} System.Exception {System.InvalidOperationException}

Task Detail: (this is inside the same controller) 任务详细信息:(在同一控制器内)

    [HttpGet]
    public ActionResult TaskDetail(int taskid)
    {
        var loggedonuser = _repo.GetCurrentUser();


        var companies = _repo.All<Company>();
        var users = _repo.All<User>();

        var task = _repo.Single<InstructionTask>
            (x => x.TaskID == taskid && (x.CompanyID == loggedonuser.CompanyID || x.AssignedTo.Contains(loggedonuser.CompanyType.ToString())));

        var dto = new TaskDTO
        {
            TaskID = task.TaskID,
            Title = task.Title,
            Description = task.Description,
            DateCreated = task.DateCreated,
            IsClosed = task.IsClosed,
            CompanyID = companies.Where(y => task.CompanyID == y.CompanyID).SingleOrDefault().Identifier,
        };

        var checkedtags = TaskTagsHelper.GetTags(task.AssignedTo);

        var t = TaskTagsHelper.GetPanelTags();

        if (checkedtags != null) //if tags are present only then tick them off...
        {
            foreach (var item in t.Keys.ToList())
            {
                if (checkedtags.Any(x => x == item))
                    t[item] = true;
            }
        }

        dto.AvailableTags = t;

        if (task.DueDate.HasValue)
            dto.DueDate = task.DueDate.Value;

        var comments = _repo.All<TaskComment>()
            .Where(x => x.TaskID == task.TaskID)
            .OrderByDescending(x => x.Timestamp)
            .Select(x => new TaskCommentDTO
            {
                Comment = x.Comment,
                Timestamp = x.Timestamp,
                CompanyID = companies.Where(y => x.CompanyID == y.CompanyID).SingleOrDefault().Identifier,
                UserID = users.Where(y => x.UserID == y.UserID).SingleOrDefault().Login,
                Type = EnumHelper.Convert<TaskCommentType>(x.Type)
            });

        dto.AllComments = comments;

        return View(new TaskViewModel
        {
            TaskDetail = dto,
            NewComment = new TaskCommentDTO()
        });
    }

I am catching the exception here in my base controller: 我在基本控制器中捕获了异常:

    //Generic methods/ controllers/ attributes will be applied here...
    protected override void OnException(ExceptionContext filterContext)
    {
        //dont interfere if the exception is already handled
        if (filterContext.ExceptionHandled)
            return;

        //let the next request know what went wrong
        filterContext.Controller.TempData["exception"] = filterContext.Exception;

        //logg exception

        //set up redirect to my global error handler
        filterContext.Result = new ViewResult { ViewName = "~/Views/Error/PublicError.aspx" };

        //advise subsequent exception filters not to interfere and stop
        // asp.net from showing yellow screen of death
        filterContext.ExceptionHandled = true;

        //erase any output already generated
        filterContext.HttpContext.Response.Clear();
    }

From what I understand of your code, you are calling the TaskDetail action from the Update method. 据我对代码的了解,您正在从Update方法调用TaskDetail操作。 This is not recommended. 不建议这样做。 Here is why: 原因如下:

  1. The owning context of the request is the Update method, which is why it's trying to render the "Update" view. 请求的拥有上下文是Update方法,这就是为什么它试图呈现“ Update”视图的原因。 That is because by convention the view is selected based on the first action that is hit. 这是因为按照惯例,视图是根据命中的第一个动作选择的。 MVC is unaware that you've called TaskDetail. MVC不知道您已调用TaskDetail。
  2. It's a violation of the PRG pattern (Post-Redirect-Get). 这违反了PRG模式(Post-Redirect-Get)。 I suggest you read up on this here: http://en.wikipedia.org/wiki/Post/Redirect/Get - Basically, you really shouldn't render anything out from an HTTP POST. 我建议您在这里阅读: http : //en.wikipedia.org/wiki/Post/Redirect/Get-基本上,您真的不应该从HTTP POST渲染任何内容。 You should redirect back to a GET. 您应该重定向回GET。 It causes all sorts of problems not doing this. 不这样做会引起各种问题。

If you want it working as it is, you can do this by changing the last line of the TaskDetail to the following so that it knows to always render the TaskDetail view, but I do not recommend it: 如果您希望它按原样工作,则可以通过将TaskDetail的最后一行更改为以下内容来做到这一点,以使它知道始终呈现TaskDetail视图,但我不建议这样做:

return View("TaskDetail", ...viewModel...)

Well the obvious question is: 好吧,显而易见的问题是:

Do you have a file in one of these folders: 这些文件夹之一中是否有文件:

Views\ControllerName\TaskDetail.aspx
Views\ControllerName\TaskDetail.ascx
Views\Shared\TaskDetail.aspx
Views\Shared\TaskDetail.ascx

Where ControllerName is the name of the controller that your Update method is in. That is, if your controller is HomeController, then your folder would be \\Views\\Home\\ 其中ControllerName是您的Update方法所在的控制器的名称。也就是说,如果您的控制器是HomeController,则您的文件夹将是\\ Views \\ Home \\

Edit: 编辑:
I'm also a little confused about your ModelState.IsValid call, but mostly because you didn't include the TaskDetail controller action. 我对您的ModelState.IsValid调用也有些困惑,但是主要是因为您没有包括TaskDetail控制器动作。 If your model state is not valid wouldn't you want to return the Update view so the user can correct their mistakes and resubmit? 如果您的模型状态无效,您是否要返回“更新”视图,以便用户可以纠正错误并重新提交? Or are they POSTing from the TaskDetail to the Update action? 还是他们从TaskDetail发布到Update操作?

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

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