本篇主要介绍 HtmlHelper,UrlHelper,QueryString传值,Helper表单,UpdateModel,单选·复选(更新)
注:
mvc项目新添加的类文件后必须编译后,才能在可视化生成view时候设置基于新添加的类为model
js文件放在模版页才能生效,在子view里放js不能生效
上节将MVC 节本类型和MVC 框架结构大致了解下,再结合本章一些方法就可以填充网页内容了,新建Form 然后get ,post数据提交,然后Action中获取并使用。
1. HtmlHelper ,UrlHelper
MVC 中常用 HtmlHelper 去创建Html元素。
如; <%=Html.ActionLink("超级链接到about","about") %>
2. QueryString 传值
即超链接显示Url?name=mike,点击后在Action中应该能获取 参数id和参数name的值
生成超链接:<%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>
userController中的edit Action 获取 参数值有几个方法。
1) 直接Request.QueryString[“name”]
2) public ActionResult Edit(string name)
只要Action方法中的参数和QueryString传来的参数名字相同就可以直接用。
3.表单
QueryString 完成了get方法,那么可以使用post来提交 获取Form了。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
user/Index页面
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>user.Index</h2>
<%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>
<h2>html.form生成表单</h2>
<% using (Html.BeginForm("edit","user",FormMethod.Post,new {id="form1"})){ %>
Name:<%=Html.TextBox("name") %>
<br />
Password:<%=Html.Password("pwd") %>
<%-- <%=Html.RadioButton("RadioButton", "d")%>请选择
<%=Html.CheckBox("CheckBox", "sdfsdf")%>--%>
<br />
<input type="submit" id="sub" value="提交" />
<% }%>
</asp:Content>
Html.BeginForm 生成form。
获取form值也有几个方法:
1) Request.Form
2) ACTIONRestult 方法参数和form name 参数相同就行。
例: Form 中有name,pwd 字段。 那么 ActionResult edti(string name,string pwd)
3)用 UpdateModel(new user{xxx},Request.Form.AllKeys);
4)
[HttpPost] //声明当post方法 就用这个action
public ActionResult Edit( FormCollection collection)
4. UpdateModel
5.单选,多选,并更新
6. 表单验证
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
user/Index页面
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>user.Index</h2>
<br />
<h2>表单提交</h2>
<fieldset>
<%=Html.ValidationMessage("u") %>
<%using (Html.BeginForm())
{ %>
<label>name</label><%=Html.TextBox("u.name")%>
<label>pwd</label><%=Html.TextBox("u.pwd")%>
<br />
<input type="submit" value="提交表单" />
<% } %>
</fieldset>
<br />
<%=Html.ActionLink("链接querystring使用","edit","user",new {name="mike"},new {@class="x"}) %>
<h2>html.form生成表单</h2>
<% using (Html.BeginForm("edit","user",FormMethod.Post,new {id="form1"})){ %>
Name:<%=Html.TextBox("name") %>
<br />
Password:<%=Html.Password("pwd") %>
<%-- <%=Html.RadioButton("RadioButton", "d")%>请选择
<%=Html.CheckBox("CheckBox", "sdfsdf")%>--%>
<br />
<input type="submit" id="sub" value="提交" />
<% }%>
</asp:Content>
public ActionResult Index(MvcApplication1.Models.User u)
{
//提交表单
if (string.IsNullOrEmpty(u.name))
{
ViewData.ModelState.AddModelError("u", "姓名不能为空");
}
//return RedirectToAction("index", "home");
//return View("Edit");
//Response.Redirect("edit");
return View();
//return View();
//return Redirect("home/index");
//return View("home/index");
}
总结:添加1个验证控件
问题更新和解决方法
msdn 帮助 VS2008 无 MVC帮助。
Aspx 无需编译修改就生效
System.Web.Mvc.HTML
Prop+tab+tab 自动生成属性
1.ASP.net MVC 设置默认调试浏览器 http://www.cnblogs.com/hipo/archive/2012/05/09/2491253.html
2.
为什么编译项目 起始 http://localhost:1692/Views/Home/Index.aspx
属性项目 ---》web-->特定页面--->设置默认 Home/Index
3自动生成属性
http://www.360doc.com/content/12/0611/10/9705579_217393104.shtml
public class User
{
public string name { get; set; }
public string pwd { get; set; };
}