【问题标题】:ASP.net MVC 5 Loading another view and stopping the current viewASP.net MVC 5 加载另一个视图并停止当前视图
【发布时间】:2016-12-21 21:15:20
【问题描述】:

MVC 新手所以这是我的问题。

如果用户属于某个类别,我想加载另一个视图。举个例子:

namespace toolSwitchboard.Controllers
{
    public class SwitchboardController : Controller
    {
        // GET: Switchboard
        public ActionResult Index()
        {
            ViewData["Message"] = "Your application description page for Switchboard.";
            ViewData["theYear"] = DateTime.Now.Year.ToString();

            getMainData();

            return View();
        }

    public ActionResult getMainData()
    {            
        if (UnboundNewClass == "NEW")
        {
            return RedirectToAction("Index", "AddPresenter"); //Latest try
        }
        else
        {
            ViewBag.Admin = false;
            ViewBag.AddP = false;
            ViewBag.MyClasses = true;
            ViewBag.Change = true;
            ViewBag.New = false;
            ViewBag.BTT = false;
            ViewBag.Eval = false;
            ViewBag.cup = false;
            ViewBag.SCHEDULE = false;
            ViewBag.Travel = false;

            return View();
        }
    }
    }
}

我在 公共类 SwitchboardController : Controller 中调用此代码。

尽管执行上述操作似乎并没有加载 AddPresenter 视图,而 仍然 加载了当前视图(称为 Switchboard)。当然,在加载 Switchboard 视图时它会出错,因为我在 Index.cshtml 页面上有一些没有任何数据的 RAZOR 代码。

我试过了:

return View("~/Views/AddPresenter/Index.cshtml");
return View("AddPresenter", "AddPresenter");
return View("../AddPresenter/Index", "AddPresenter");
return View("~/AddPresenter/Index", "AddPresenter");
return View("~/AddPresenter/Index");
return RedirectToAction("AddPresenter", "AddPresenter", null);
return RedirectToAction("Index", "AddPresenter");

似乎没有一个有效。他们只是加载 Switchboard Index.cshtml 页面而不是所需的 AddPresenter Index.cshtml 页面。

这是我的总机视图,其中有错误:

//More code above....
<div id="wrapper">
    <div id="yellowArea">
        <p class="boxStyle">Presentation:</p>
        <Button class="btn btn-primary btn-large btnCustom1" id="NewT">TRAVEL</Button>
        <Button class="btn btn-primary btn-large btnCustom2" id="NewE">External</Button>
        <Button class="btn btn-primary btn-large btnCustom3" id="Credits">Credits</Button>

        @if (ViewBag.BTT)
        {
            <Button class="btn btn-primary btn-large btnCustom4" id="BTT">BTT</Button>
        }
    </div>
    <div id="clearArea1">
    //More code below....

错误出现在 ViewBag.BTT 行,因为我只在他们只查看 Switchboard View 而不是 AddPresenter View。

为了以加载视图的方式更正此问题,我可以做哪些事情?

【问题讨论】:

  • 你确定presenter == "NEW" 是真的吗?
  • @Steve 是的,先生。在那里休息了一下,它确实遵循了这条 if 路径。
  • 尝试返回 View("~/Views/AddPresenter/Index")。虽然我认为重定向应该有效
  • 不清楚您的要求。如果presenter的值是"NEW",您将被重定向(假设您不使用ajax),否则您将返回当前视图。但是你没有在return View() 语句中返回一个模型,如果你引用它的属性可能会产生错误
  • 你的重定向转到AddPresenterControllerAddPresenter() 方法(而不是Index 视图)

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

如果你想加载另一个 view 你只需传递视图的名称。

例如:

if (presenter == "NEW")
{
    return View("AddPresenter");
} else 
{
    return View();
}

如果presenter == "NEW",ASP.NET 将在"Views\NameOfTheController" 中搜索。 如果给定的视图名称不存在,搜索将在“视图\共享”中继续。

如果presenter != "NEW",ASP.Net 将遵循相同的规则来查找视图。标准视图名称是ActionName(或控制器中的Method

如果你想重定向到另一个控制器,使用this重载RedirectToAction

return RedirectToAction("Index","NameOfTheController");

这会将您带到/NameOfTheController/Index。 在你的情况下:

Return RedirectToAction("Index","AddPresenter");

【讨论】:

  • 这似乎产生了同样的错误(并且仍在加载 Switchboard View)。
【解决方案2】:

这是你的主控制器,你的总机:

namespace toolSwitchboard.Controllers
{
    public class SwitchboardController : Controller
    {
        // GET: Switchboard
        public ActionResult Index()
        {
            ViewData["Message"] = "Your application description page for Switchboard.";
            ViewData["theYear"] = DateTime.Now.Year.ToString();

            getMainData();

            return View();
        }

    public ActionResult getMainData()
    {            
        if (UnboundNewClass == "NEW")
        {
            return RedirectToAction("Index", "AddPresenter"); //Will Redirect you to AddPresenterController.Index()
        }
        else
        {
            ViewBag.Admin = false;
            ViewBag.AddP = false;
            ViewBag.MyClasses = true;
            ViewBag.Change = true;
            ViewBag.New = false;
            ViewBag.BTT = false;
            ViewBag.Eval = false;
            ViewBag.cup = false;
            ViewBag.SCHEDULE = false;
            ViewBag.Travel = false;

            return View();
        }
    }

    }
}

您的 AddPresenter 类:

public class AddPresenterController : Controller
{
    public ActionResult Index()
    {
        return View(); // Potentially put view name here as parameter
    }
}

我想让你检查的一些事情是:

1) 您的 Views 文件夹中有一个名为 AddPresenter 的文件夹。为了让控制器访问视图,它必须在他们自己的文件夹中(Views->ControllerName)或在视图下的共享文件夹中。

2) 在 Views->AddPresenter 文件夹中,您有一个名为 Index 的视图,如果不是,您需要将视图名称传递给 AddPresenters 返回的 Index 方法。

3) 你的路由(AppStart->RouteConfig) 有默认路由。

【讨论】:

    【解决方案3】:

    好吧,我终于把它修好了。这都是由 Switchboard ActionResult Index() 方法中的 return View(); 引起的。

    将该代码更改为:

    public ActionResult Index()
    {
        ViewData["Message"] = "Your application description page for Switchboard.";
        ViewData["theYear"] = DateTime.Now.Year.ToString();
    
        return getMainData();
    }
    

    而不是这个:

    public ActionResult Index()
    {
        ViewData["Message"] = "Your application description page for Switchboard.";
        ViewData["theYear"] = DateTime.Now.Year.ToString();
    
        getMainData();
    
        return View();
    }
    

    允许我加载 AddPresenter 视图,而不会仍然加载 Switchboard 视图并导致错误。

    使用 return RedirectToAction("Index", "AddPresenter"); 解决了我尝试过的所有方法:

        if (UnboundNewClass == "NEW")
        {
            return RedirectToAction("Index", "AddPresenter");
        }
        else
        {
            ViewBag.Admin = false;
            ViewBag.AddP = false;
            ViewBag.MyClasses = true;
            ViewBag.Change = true;
            ViewBag.New = false;
            ViewBag.BTT = false;
            ViewBag.Eval = false;
            ViewBag.cup = false;
            ViewBag.SCHEDULE = false;
            ViewBag.Travel = false;
    
            return View();
        }
    

    感谢所有提供帮助和煽动的人! :o)

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 2018-09-05
      • 2015-11-23
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多