天天看點

ASP.NET MVC5 知識點整理

1、MVC5 View視圖中建立帶樣式超連結

 @Html.ActionLink("action名","controller名",new { id=item.ID },new{style="color:red",@class="css樣式名"})

@Html.LabelFor(model => model.Genre, new { @class = "control-label" })

@Html.ValidationMessageFor(model => model.Genre, null, new { @class = "help-inline" })

2、Model中字段的顯示設定及類型指定

添加引用

using System.ComponentModel.DataAnnotations;

[Display(Name = "釋出日期")]

[DataType(DataType.Date)]

public DateTime ReleaseDate { get; set; }

Display 特性指定了顯示的字段名(本例中“釋出日期”替換了“ReleaseDate”)。

DataType 特性指定了資料類型,在本例中它是日期類型,是以存儲在該字段的時間資訊将不會顯示出來。

3、

[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Edit(Movie movie)

{

    if (ModelState.IsValid)

    {

        db.Entry(movie).State = EntityState.Modified;

        db.SaveChanges();

        return RedirectToAction("Index");

    }

    return View(movie);

}

HttpPost特性指定隻有POST請求才能調用這個Edit方法。HttpGet是預設值,無需指定。

ValidateAntiForgeryToken 這個特性用來阻止僞造的請求,它和視圖(Views\Movies\Edit.cshtml)中的

@Html.AntiForgeryToken() 是成對出現的。

@using (Html.BeginForm()) {

    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true)