【发布时间】: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()语句中返回一个模型,如果你引用它的属性可能会产生错误 -
你的重定向转到
AddPresenterController的AddPresenter()方法(而不是Index视图)
标签: c# asp.net asp.net-mvc razor asp.net-mvc-5