日期:17-3-7
控制器三個約定:
- 控制器命名規(guī)范:"NameController",以Controller結(jié)尾
- 控制器必須是非靜態(tài)類
- 實現(xiàn)IController接口(多次繼承)
控制器里面的方法都被稱為Action
Views文件夾下面會根據(jù)Controller名新建若干個以Controller命名的文件夾萧芙,該文件夾下還能斋日,只能新建一個文件夾“Shared”怜跑,以及一個系統(tǒng)生成的web.config忘嫉。
添加視圖:在Controller的方法名上右擊添加視圖
WebForm與Asp.net MVC請求頁面的區(qū)別:
<b>webForm</b>請求的是一個aspx頁面媚朦。 http://localhost/a.aspx
<b>Asp.net MVC</b> 請求地址,請求控制器里面的方法侯嘀。http://localhost/Home/index
用戶 > Controller-Action > ViewData數(shù)據(jù)-View
Action:
- 處理用戶的請求,Request谱轨,Response
- 調(diào)用業(yè)務(wù)邏輯(Model BLL DAL)
- 把數(shù)據(jù)傳遞給View進(jìn)行展示
ViewData[]從Controller向View傳遞數(shù)據(jù)
Action 如果沒有指定(return View("index");)對應(yīng)的視圖來展示數(shù)據(jù)的話戒幔,默認(rèn)是尋找跟Action同名的View進(jìn)行展示。一般Action名與指定的視圖同名土童。
前臺表單代碼
<form action="/UserInfo/ProcessUserInfo" method="post">
<div class="form-group">
<label for="UserName">UserName</label>
<input type="text" class="form-control" name="txtName" id="UserName" placeholder="請輸入用戶名">
</div>
<div class="form-group">
<label for="Pwd">Password</label>
<input type="password" class="form-control" name="txtPwd" id="Pwd" placeholder="請輸入密碼">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
從前臺獲取數(shù)據(jù)的四種方式
方式一
public ActionResult ProcessUserInfo()
{
string UserName = Request["txtName"];//與前臺name同名诗茎。對于表單form,只有設(shè)置了 name 屬性的表單元素才能在提交表單時傳遞它們的值。
string UserPwd = Request["txtPwd"];
return Content("OK" + "</br>" + UserName + "</br>" + UserPwd);
}
方式二
public ActionResult ProcessUserInfo(FormCollection collection)
{
string str = collection["txtName"];
string pwd=collection["txtPwd"];
return Content("OK" + "</br>" + str+ "</br>" + pwd
}
方式三
public ActionResult ProcessUserInfo(string txtName,string txtPwd)//與前臺name同名
{
return Content("OK" + "</br>" + txtName + "</br>" + txtPwd)
}
方式四
public class Info
{
public string txtName { get; set; } //與前臺name同名
public string txtPwd { get; set; }
}
public ActionResult ProcessUserInfo(Info userA)
{
return Content("OK" + "</br>" + userA.txtName,string + "</br>" + userA.txtPwd)
}
HtmlHelper
超鏈接的三種形式
超鏈接方式1
<a href="/Home/About">鏈接到About頁面</a>
<br/>
超鏈接方式2
這種方式避免了上面更改路由機(jī)制之后要更改所有的鏈接代碼
<br/>
<a href=@Url.Action("About","Home")>鏈接到About</a>
<br/>
超鏈接方式3
<br/>
@Html.ActionLink("About頁面","About","Home",null,new { style = "Color:green" ,@class="aaa"}) 設(shè)置htmlAttr