首页 > 代码库 > Asp.net MVC3 Razor语法小记

Asp.net MVC3 Razor语法小记

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 = http://www.mamicode.com/a.ColumnID.ToString(),
                                             Text = a.name
                                         };
List<SelectListItem> Items = new List<SelectListItem>();
Items.Add(new SelectListItem { Value =http://www.mamicode.com/ "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 = "http://www.mamicode.com/1" }, new SelectListItem { Text = "中学", Value = "http://www.mamicode.com/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="http://www.mamicode.com/@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="http://www.mamicode.com/@tab.Url">@tab.Text</a>
        </li>
    }   
}
@PrintTab(tab, (tab.TabId == Model.ActiveTab));

Asp.net MVC3 Razor语法小记