TextBoxRazor: @model Student//傳遞一個null值@Html.TextBox("StudentName", null, new { @class = "form-control" })
//使用一個字符串,作為值@Html.TextBox("StudentName", "this is value", new { @class = "form-control" }
//Controller:ViewBag.StudentName="Janes";//使用ViewBag獲取數(shù)據(jù)@Html.TextBox("StudentName", (string)ViewBag.StudentName, new { @class = "form-control" })
//Controller: StudentName="Janes";//使用Model獲取數(shù)據(jù)@Html.TextBox("StudentName", Model.StudentName, new { @class = "form-control" })
Html: <input class="form-control" id="StudentName" name="StudentName" type="text" value="" /><input class="form-control" id="StudentName" name="StudentName" type="text" value="this is value" /><input class="form-control" id="StudentName" name="StudentName" type="text" value="Janes" /><input class="form-control" id="StudentName" name="StudentName" type="text" value="Janes" />
TextBoxForRazor: @model Student//Controller: StudentName="Janes";@Html.TextBoxFor(m => m.StudentName, new { @class = "form-control" })
Html: <input class="form-control" id="StudentName" name="StudentName" type="text" value="Janes" />
TextBox和TextBoxFor的區(qū)別: @Html.TextBox是一個弱類型,@Html.TextBoxFor()是個強類型。 @Html.TextBox參數(shù)是一個字符串,@Html.TextBoxFor參數(shù)是一個lambda表達式。 @Html.TextBox如果屬性拼寫錯誤在編譯的時候不會報錯,只能在運行才會報錯。 @Html.TextBoxFor在編譯的時候會提示錯誤。
|