MVC 4 CONTROLLER

本文关键字:CONTROLLER MVC | 更新日期: 2023-09-27 18:30:56

我试图在这里制作一个双重视图状态,首先我创建一个布尔值来检查用户是否经过身份验证,如果是,它会将我发送到一个视图,如果不是,它会将我发送到另一个视图。问题是我收到此错误:

传递到字典中的模型项的类型为 "System.Web.Mvc.ViewResult",但此字典需要一个模型项 类型为TheNonViolenceProject.Models.ViewModels.PageViewModel'1[TheNonViolenceProject.Models.Pages.EducationPage]'。

这是我的代码:

public class EducationPageController : PageController<EducationPage> {
    private bool isUserLoggedin = true;
    public ActionResult Index(EducationPage currentPage) {
        PageViewModel<EducationPage> model = PageViewModel.Create(currentPage);
        if (isUserLoggedin) {
            return View(IndexIsAuthenticated(currentPage));
        }
        return View(model);
    }
    public ActionResult IndexIsAuthenticated(EducationPage currentPage {
        //isUserLoggedin = User.Identity.IsAuthenticated;
        PageViewModel<EducationPage> modelIsAuthenticated = PageViewModel.Create(currentPage);
        return View(modelIsAuthenticated);
    }
}

MVC 4 CONTROLLER

更改

    PageViewModel<EducationPage> model = PageViewModel.Create(currentPage);
    if (isUserLoggedin)
    {
        return View(IndexIsAuthenticated(currentPage));
    }

    PageViewModel<EducationPage> model = PageViewModel.Create(currentPage);
    if (isUserLoggedin)
    {
        return RedirectToAction("IndexIsAuthenticated", model);
    }

我猜这条线

return View(IndexIsAuthenticated(currentPage));

应该是

return IndexIsAuthenticated(currentPage);