MVC簡單筆記

來源:互聯網
上載者:User
1】基本文法@{    int num1 =10;
    int num2 =5;
    int sum = num1 + num2;
    @sum;
    @(num1 +num2);
    string color ="Red";
    <font color="@color">@sum</font>
}

2】特殊文法
    輸出@符號:@@
    輸出Email地址:Razor模板會自動識別出Email地址,所以不需要我們進行任何的轉換。而在代碼塊中,只需要使用 @:Tom@gmail.com 即可。@:表示後面的內容為文本。
    輸出HTML代碼(包含標籤):直接輸出,string html = "<font color='red'>文本</font>"; @html
    輸出HTML內容(不包含標籤):有兩種方法,第一種:IHtmlString html=new HtmlString("<font color='red'>文本</font>"); @html; 第二種:string html = "<font color='red'>文本</font>"; @Html.Raw(html);

3】輸出資料行表@foreach (var p in Model) {
    <div class="item">
        <h3>@p.Name</h3>
        @p.Description
        <h4>@p.Price.ToString("c")</h4>
    </div>
}

4】輸出使用者控制項@foreach (var p in Model.Products) {
    Html.RenderPartial("ProductSummary", p);
}

5】在controller中引用使用者控制項需要傳遞資料時,可以使用下列方法
public ViewResult Summary(Cart cart) {
    return View(cart);
}
輸出時: @{Html.RenderAction("Summary", "Cart");}
public PartialViewResult Menu() {
    IEnumerable<string> categories = repository.Products
            .Select(x => x.Category)
            .Distinct()
            .OrderBy(x => x);
    return PartialView(categories);
}
輸出時: @{ Html.RenderAction("Menu", "Nav"); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.