MVC之htmlhelper

View

@model 知识点.Models.Pig

@{
Layout = null;
}
@using 知识点


<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>

<div>
@* <form action="/C01HtmlHelperDemo/Edit" method="post">
<input type="submit" value="提交"/>
</form>*@

生成表单的方式

@using (Html.BeginForm("Edit", "C01HtmlHelperDemo", FormMethod.Post))
{
@Html.TextBoxFor(c => c.Name)<br />
@Html.PasswordFor(c => c.Pwd) <br />
@Html.CheckBoxFor(c => c.Gender)<br />
@Html.RadioButtonFor(c => c.Color, "1")
@Html.RadioButtonFor(c => c.Color, "2")

<br />
@Html.DropDownListFor(c => c.Color, new List<SelectListItem> {
new SelectListItem{ Text="黑色", Value="1"},
new SelectListItem{ Text="白色", Value="2"},
})
<br />

@Html.ActionLink("跳转1","Edit","C01HtmlHelperDemo")

@Html.Submit("提交")
}
</div>
</body>
</html>

 

Contorller

public class C01HtmlHelperDemoController : Controller
{
//
// GET: /C01HtmlHelperDemo/

public ActionResult Edit()
{
Pig pig = new Pig { Name = "家猪", Age = 1, Color = 2, Gender = false, Pwd = "123" };

return View(pig);
}

}

 

 

Model

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public class Pig
{
[Required(ErrorMessage = "非空")]
public string Name { get; set; }
public int Age { get; set; }
public string Pwd { get; set; }
public bool Gender { get; set; }
public int Color { get; set; }
}

posted on 2015-08-31 22:34  YAO000  阅读(112)  评论(0)    收藏  举报