1,
@Html.TextBox("txt", "hello", new { style = "width:1000px;height:1000px;" })
生成标签:
<input id="txt" name="txt" style="width:1000px;height:1000px;" type="text" value="hello" />
2,
特殊属性(class:c#保留关键字)
@Html.TextBox("txt", "hello", new { style = "width:1000px;height:1000px;",@class="aa" })
生成标签:
<input class="aa" id="txt" name="txt" style="width:1000px;height:1000px;" type="text" value="hello" />
3,
带连接字符的属性(例如:data_result)
@Html.TextArea("text", "hello", new { data_result ="data"})
生成标签:
<textarea cols="20" data-result="data" id="text" name="text" rows="2">hello</textarea>
4,
@using (Html.BeginForm()) { <input type="submit" value="Create" /> }
生成标签:
<form action="/storemanager" method="post">
<input type="submit" value="Create" />
</form>
==================添加输入元素
----@Html.TextBox
@Html.TextBox("Title", string.Empty) //单行输入
生成标签:
<input id="Title" name="Title" type="text" value="" />
----@Html.TextArea
@Html.TextArea("Title", string.Empty) //多行输入
生成标签:
<textarea cols="20" id="Title" name="Title" rows="2">
----@Html.Label
@Html.Label("Title","请输入:")
生成标签:
<label for="Title">请输入:</label>
---- @Html.DropDownList
控制器代码:
public ActionResult Create2() { //单选 集合 ,值字段,文本字段,选定的值 ViewBag.Genre = new SelectList(db.Genres, "GenreId", "Name",1); return View(); }
视图代码:
@Html.DropDownList("Genre",string.Empty)
---- @Html.ListBox(可以多项选择)
@{ List<string> listbox = new List<string>(); listbox.Add("a"); listbox.Add("b"); listbox.Add("c"); } @Html.ListBox("listitem", new SelectList(listbox,"a"))
控制器代码:
//public ActionResult Create2() { //单选 集合 ,值字段,文本字段,选定的值 ViewBag.Genre = new SelectList(db.Genres, "GenreId", "Name",1); //多选 ViewBag.Genre2 = new MultiSelectList(db.Genres, "GenreId", "Name", new List<string>() { "1", "2" }); return View(); }
视图代码:
@Html.ListBox("Genre") @Html.ListBox("Genre2")
----@Html.ValidationMessage()
控制器代码:
public ActionResult Create3() { return View(); } [HttpPost] public ActionResult Create3(string text) { if (string.IsNullOrEmpty(text)) { ModelState.AddModelError("text", "错误"); return View(); } else { return RedirectToAction("Index"); } }
视图代码:
@{ ViewBag.Title = "Create3"; } <h2>Create3</h2> @using (Html.BeginForm()) { <li>@Html.TextBox("text")</li> <li>@Html.ValidationMessage("text")</li> <li><input type="submit" value="提交" /></li> }