一、Controllers控制器
public ActionResult Index()
{
ViewBag.TxtName = "文本值";
ViewBag.wb = "文本内容";
//设置girl单选按钮被选中
ViewBag.dx = "girl";
return View();
}
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
if(formCollection["tj"] != null)
{
//获得多选框
string StrCheckBox ="";
bool MyCheckBox1 = formCollection["MyCheckBox1"].Contains("true");
bool MyCheckBox2 = formCollection["MyCheckBox2"].Contains("true");
if (MyCheckBox1)
{
StrCheckBox += "看书 ";
}
if (MyCheckBox2)
{
StrCheckBox += "跑步 ";
}
return Content(StrCheckBox);
}
}
二、View视图
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
string dx = ViewBag.dx.ToString(); //单选
<div>
文本框:@Html.TextBox("TxtName")
</div>
<div>
密码框:@Html.Password("TxtMm")
</div>
<div>
多行文本:@Html.TextArea("wb",new {id = "textarea1", cols="20", rows="5", wrap="virtual"})
</div>
<div>
单选:@Html.RadioButton("radio", "boy", dx == "boy") 男
@Html.RadioButton("radio", "girl", dx == "girl") 女
</div>
<div>
多选:@Html.CheckBox("MyCheckBox1", false, new { id = "checkBox1"})看书
@Html.CheckBox("MyCheckBox2", false, new { id = "checkBox2"})跑步
@* <input type="checkbox" name="CheckBox2" value="ks" />看书
<input type="checkbox" name="CheckBox2" value="pb" />跑步*@
</div>
<div>
<input type="submit" value="提交" name="tj"/>
</div>
}
推荐阅读:
HtmlHelper用法大全2:TextBox、Password、TextArea、RadioButton、CheckBox
HtmlHelper用法大全3:Html.LabelFor、Html.EditorFor、Html.RadioButtonFor、Html.CheckBoxFor