假设我有 _Layout.cshtml
我有这个:
@if (ViewBag.SignUpURL == null)
{
<a href="@Url.Content("~/Home/Join")" ><span>
<strong>JOIN NOW!</strong></span></a>
}
else
{
<a href="@ViewBag.SignUpURL" ><span>
<strong>JOIN NOW!</strong></span></a>
}
当我尝试在
下填充ViewBag.SignUpURL
时
public ActionResult Index()
{
ViewBag.SignUpURL = HttpContext.Request.Url.AbsoluteUri.Replace("Index", "SignUp");
return View();
}
Index
View 使用 _Layout.cshtml
。
什么也没发生...
一般来说,我需要以某种方式将数据从使用它的页面传递到Layout
页面。
可以吗?
谢谢!
更新:
最佳答案
您应该考虑使用 Html.RenderAction() ,然后它将返回到您指定的 Controller 操作,并且您将能够返回带有模型的部分 View :
查看:
@Html.RenderAction("GetSignUp")
Controller 操作
public ActionResult GetSignUp()
{
//do your logic here
return PartialView("_SignUp");
}
关于c# - 如何从使用布局的 View 更改 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13502937/