一MVC2的view页面中form的用法: <% using (Html.BeginForm("方法名称", "控制器名称", FormMethod.Post)) {%>
二 视图把数据传到控制器的2种方法:
例: 视图代码
<% using (Html.BeginForm("About","Home"))
{ %>
<%=Html.TextBox("Name")%>
<input type="submit" value="submit" name="submit" />
<%}%>
控制器代码
(1) public ActionResult About(FormCollection form)
{
string s = form["Name"];
return View();
}
(2)public ActionResult About()
{
string s = Request.Form["Name"] ;
return View();
}
三 Html.ActionLink的用法
以下使用参数文字说明:
- linkText:生成的链接所显示的文字 类型:string
- actionName:对应控制器的方法 类型:string
- routeValues:向对应的action传递的参数 类型:object 或 RouteValueDictionary
- controlName:指定控制器的名称 类型:string
- htmlAttributes:设置<a>标签的属性 类型:object 或 IDictionary
- protocol:指定访问协议如:http等 类型:string
- hostName:指定访问域名 类型:string
- fragment:指定访问锚点 类型:string
重载一:Html.ActionLink("linkText","actionName")【默认当前页面控制器】
调用:<%: Html.ActionLink("默认当前页面控制器", "About")%>
生成效果:<a href="/Home/About">默认当前页面控制器</a>
重载二:Html.ActionLink("linkText","actionName",routeValues)
调用: routeValues Is object:
<%: Html.ActionLink("默认当前页面控制器", "About", new { id = 1, type = "Dic" })%>
routeValues Is RouteValueDictionary:
<%RouteValueDictionary Dictionary = new RouteValueDictionary();
Dictionary["id"] = 1;
Dictionary["type"] = "Dic";
%>
<%: Html.ActionLink("默认当前页面控制器", "About", Dictionary)%>
生成效果:<a href="/Home/About?classid=1">默认当前页面控制器</a>
重载三:Html.ActionLink("linkText","actionName","controlName")
调用:<%: Html.ActionLink("默认当前页面控制器", "About", "Home")%>
生成效果:<a href="/Home/About">默认当前页面控制器</a>
重载四:Html.ActionLink("linkText","actionName",routeValues,htmlAttributes)
调用: htmlAttributes Is object:
<%: Html.ActionLink("首页", "Index", "Home", null, new { @class = "active", target = "_blank" })%>【注:由于class是保留关键字,所以一定要写成@class】
htmlAttributes Is IDictionary:
<%IDictionary<string, object> AttrDictionary = new Dictionary<string, object>();
AttrDictionary["class"] = "active";
AttrDictionary["target"] = "_blank";
%>
生成效果:<a class="active" href="/" target="_blank">首页</a>
重载五:Html.ActionLink("linkText","actionName","controlName","protocol","hostName","fragment",routeValues,htmlAttributes)
调用:<%: Html.ActionLink("关于我们", "About", "Home", "http", "localhost", "top", null, null)%>
生成效果:<a href="http://localhost:12120/Home/About#top">关于我们</a>
1 DropdownList用法
(1) Controller 控制器中代码
public SelectList GetList()
{
List<SelectListItem> itemList = new List<SelectListItem>();
List<Test> testList = tableTest.Test.ToList();
SelectListItem item;
foreach(Test t in testList)
{
item = new SelectListItem() {Text=t.name,Value=t.id.ToString()};
itemList.Add(item);
}
SelectList List = new SelectList(itemList,"value","text");
return List;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult TestEdit(int id)
{
Test t = tableTest.Test.First(m => m.id == id);
ViewData["List"] = GetList();
return View(t);
}
(2)视图页面(View)代码
<div class="editor-field">
<%= Html.DropDownListFor(model => model.age,ViewData["List"] as SelectList)%>
<%= Html.ValidationMessageFor(model => model.age)%>
</div>
2 RadioButton用法:
<%=Html.RadioButtonFor(m=>m.age,0,new{@id = "radio1", @name = "rdolstState",@checked=true }) %>
<%=Html.RadioButtonFor(m => m.age, 1, new