码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net MVC3 Razor语法小记

时间:2016-10-14 22:45:12      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

Asp.net MVC3 Razor语法小记

 

1.在MVC View中使用 三元运算符的方式:@(bool?"":"")

2.在@{}中输出一行:@:<html>.如果这一行的上下文在程序代码中则用:@Html.Raw(string.Format(""));而不是Html.Raw();

3.Radio默认选中: @Html.RadioButtonFor(m => m.BookType, 0, new { @checked="checked"})小学

4.DropDownList添加非数据库数据项:

IEnumerable<SelectListItem> ColumnList = from a in Company.Column.ToList()
                                         where a.parentID==0
                                         select new SelectListItem
                                         {
                                             Value = a.ColumnID.ToString(),
                                             Text = a.name
                                         };
List<SelectListItem> Items = new List<SelectListItem>();
Items.Add(new SelectListItem { Value = "0", Text = "一级栏目" });
Items.AddRange(ColumnList); 
ViewBag.ColumnList = Items;
。。。。

***直接使用后台ViewBag:
@Html.DropDownList("groupid", new SelectList((IEnumerable<SelectListItem>)ViewBag.GroupList, "groupid", "groupname"));
***添加静态数据项:
@Html.DropDownListFor(model => model.GroupCategoryID,new SelectListItem[] { new SelectListItem { Text = "小学", Value = "1" }, new SelectListItem { Text = "中学", Value = "2" } })
5.在页面中输出javascript变量值:
<script language="JavaScript" type="text/javascript">
        var teacher = @Html.Raw(HttpUtility.HtmlDecode(teacherStr.ToString())); 
  

。。。。

6.使用@helper


@helper PrintTab(Tab tab, bool active)
{
    if(active)
    {
        <li class="active">
            <a href="@tab.Url">@tab.Text</a>

            @if(tab.Closable)
            {
                <span class="button-tab-close">&times;</span>
               
                using (Html.BeginForm("Close", "Tab", FormMethod.Post, new { id = "closeTab" }))
                {
                    @Html.Hidden("tabId", tab.TabId)
                }
            }
        </li>
    }
    else
    {
        <li>
            <a href="@tab.Url">@tab.Text</a>
        </li>
    }   
}
@PrintTab(tab, (tab.TabId == Model.ActiveTab));

Asp.net MVC3 Razor语法小记

标签:

原文地址:http://www.cnblogs.com/gobuild/p/5961989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!