开发者

View not found - asp.net mvc error

开发者 https://www.devze.com 2023-03-26 08:33 出处:网络
I am getting an error saying the view Update has not been found, I wanted to show the TaskDetail view not the update view...

I am getting an error saying the view Update has not been found, I wanted to show the TaskDetail view not the update view...

    [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. 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. 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.
  2. It's a violation of the PRG pattern (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. You should redirect back to a 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:

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\

Edit:
I'm also a little confused about your ModelState.IsValid call, but mostly because you didn't include the TaskDetail controller action. 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消