c# - 如何从使用布局的 View 更改 html

标签 c# .net asp.net-mvc asp.net-mvc-3

假设我有 _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页面。

可以吗?

谢谢!

更新:

enter image description here

最佳答案

您应该考虑使用 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/

相关文章:

asp.net-mvc - 在 ASP.NET MVC 2 中测试模型绑定(bind)

javascript - MVC4 ajax调用并正确返回数据

c# - 从数据库加载后获取黑色图像

C# 在垃圾收集之前查找特定类的未使用对象

c# - 对象类原始类型堆栈和堆

asp.net-mvc - 为什么我的MVC4项目在bin文件夹下没有debug和release文件夹?

c# - 如何在 LINQ-to-SQL 中模拟正则表达式

c# - 编程语言中的字节顺序

c# - .NET 中是否可以使用不可变数组?

.Net:静态类和多线程?