如何在 Razor 中声明局部变量? [英] How to declare a local variable in Razor?

查看:28
本文介绍了如何在 Razor 中声明局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 asp.net mvc 3 中开发 Web 应用程序.我对它很陌生.在使用 razor 的视图中,我想声明一些局部变量并在整个页面中使用它.这怎么办?

能够执行以下操作似乎相当微不足道:

@bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);@if (isUserConnected){//表示查看用户还没有被保存<div><div>点击加入我们

<a id="登录" href="javascript:void(0);"style="display: inline; ">join</a>

}

但这行不通.这可能吗?

解决方案

我觉得你已经很接近了,试试这个:

@{bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);}@if (isUserConnected){//表示查看用户还没有保存所以继续<div><div>点击加入我们

<a id="登录" href="javascript:void(0);"style="display: inline; ">join here</a>

}

I am developing a web application in asp.net mvc 3. I am very new to it. In a view using razor, I'd like to declare some local variables and use it across the entire page. How can this be done?

It seems rather trivial to be able to do the following action:

@bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);
@if (isUserConnected)
{ // meaning that the viewing user has not been saved
    <div>
        <div> click to join us </div>
        <a id="login" href="javascript:void(0);" style="display: inline; ">join</a>
    </div>
}

But this doesn't work. Is this possible?

解决方案

I think you were pretty close, try this:

@{bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);}
@if (isUserConnected)
{ // meaning that the viewing user has not been saved so continue
    <div>
        <div> click to join us </div>
        <a id="login" href="javascript:void(0);" style="display: inline; ">join here</a>
    </div>
}

这篇关于如何在 Razor 中声明局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆