顯示具有 Razor 標籤的文章。 顯示所有文章
顯示具有 Razor 標籤的文章。 顯示所有文章

2016年12月4日 星期日

[MVC][Razor][C#] How to populate dropdown list with numbers


A note to myself


@Html.DropDownListFor(m=>m.Name, Enumerable.Range(1, 10).Select(i => new SelectListItem { Text = i.ToString(), Value = i.ToString() }))



@Html.DropDownListFor(m => m.NumberOfTickets, Enumerable.Range(1, 10).Select(i => new SelectListItem { Text = i.ToString(), Value = i.ToString() }))



Ref:
1. How to populate dropdown list with numbers
http://stackoverflow.com/questions/29659911/asp-net-mvc-how-to-populate-dropdown-list-with-numbers
2. SelectList for years
http://stackoverflow.com/questions/13959098/selectlist-for-years

2016年8月21日 星期日

[ASP.NET MVC] Razor @helper語法

Reference: http://blog.sanc.idv.tw/2011/08/aspnet-mvc-razorhelper.html

Just a note to myself...

test.cshtml


<!--寫法1-->
<div>
    @if (int.Parse(ViewData["score"].ToString()) >= 60)
    {
        <span>合格!</span>
    }
    else
    {
        <span>不合格!</span>
    }
</div>

<!--寫法2: 寫成 helper-->

@helper DisplayScore(int score)
{
    if (score >= 60)
    {
        <span>合格!</span>
    }
    else
    {
        <span>不合格!</span>
    }
<div>
    @DisplayScore(int.Parse(ViewData["score"].ToString()))
</div>


<!--寫法3: 不同page都要使用它時, 把 helper 寫在App_Code裡-->
@RivaFuns.DisplayScore(int.Parse(ViewData["score"].ToString()))



App_Code > RivaFuns.cshtml



@helper DisplayScore(int score)
{
    if (score >= 60)
    {
        <span>合格!</span>
    }
    else
    {
        <span>不合格!</span>
    }