MVC小技巧
1.創(chuàng)建控件,MVC中通過(guò)htmlHelper生成HTML標(biāo)簽。
1 <%= Html.TextBox("UserName")%>
2 <%= Html.TextBox("UserName","Coolin")%>
3 <%= Html.TextBox("UserName","Coolin",
??????????? new { @class = "className",disabled = true })%>
最后一項(xiàng)htmlAttributes,可以設(shè)置樣式等屬性。
2.RegisterRoutes帶來(lái)了什么變化。通過(guò)VS創(chuàng)建一個(gè)MVC應(yīng)用程序,會(huì)在Global.asax中看到下面的代碼(我添加了第一條)
1 routes.MapRoute(
2???????? "Account",??
3???????? "Account/List/{type}",????????????????????????????????
4???????? new { controller = "Account", action = "List" }?
5?? );
6
7 routes.MapRoute(
8???????? "Default",??????????????????????????????????????????????????????????
9???????? "{controller}/{action}/{id}",????????????????????????????????
10???????? new { controller = "Home", action = "Index", id = "" }?
11?? );
現(xiàn)在看一下它帶來(lái)的變化,假設(shè)view有如下代碼
1 <%= Html.ActionLink("男性", "List", new { type = "Man" }) %>
2 <%= Html.ActionLink("女性", "List", new { type = "Woman" }) %>
或
1 <%= Url.RouteUrl("Account", new { controller = "Account", action = "List", type= "Man" }) %>
2 <%= Url.RouteUrl("Account", new { controller = "Account", action = "List", type= "Woman" }) %>
對(duì)應(yīng)的Url應(yīng)該是? localhost:XXXX/Account/List/Man 和 localhost:XXXX/Account/List/Woman
當(dāng)然也可以不在Global.asax中注冊(cè)那條規(guī)則,對(duì)應(yīng)的Url就會(huì)變成大家熟悉的 localhost:XXXX/Account/List?type=Man。
順便提一下我在開(kāi)發(fā)中遇到過(guò)的一個(gè)問(wèn)題,還以上面的例子為例,在程序的開(kāi)發(fā)階段,沒(méi)有加入剛才那條規(guī)則,當(dāng)我們?cè)诔绦蛑屑尤肓?sitemap(mvcsitemap),結(jié)果卻是sitemap的路徑永遠(yuǎn)不能指向Woman的路徑,可能是sitemap不能通過(guò) ?后面參數(shù)區(qū)分路徑,后來(lái)加入了這條規(guī)則,url中去掉參數(shù),問(wèn)題解決了。
3.ajax異步請(qǐng)求controller
controller中有如下代碼
1 public ActionResult Delete(Guid id)
2 {
3????? Delete(id);
4????? return Json(new { success = true });
5 }
view中代碼如下
1 $.getJSON(<%= Url.Action("Delete", "Account",new { id="xx-xx-xxx" }) %>,
2???? function(result) {
3???????? if (result.success) {
4???????????? //通過(guò)腳本移除此行
5???????????? alert("刪除成功!")???
6???????? }
7???? });
?
4.在view與Mvc view user control中使用強(qiáng)類型。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
??? Inherits="System.Web.Mvc.ViewPage<Account>" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<Account>>" %>
在view 或 Mvc view user control中的代碼如下
<% if (Model != null) { %>
????? <div><%= Model.Name %></div>
????? <div><%= Model.Age %></div>
<% } %>
在controller中需要將Model賦值
public ActionResult ShowAccount()
{
????? var account = new Account { Name = "Name" , Age = 18 };
????? return View(account);
}
順便一提:
當(dāng)我需要呈現(xiàn)View的時(shí)候我會(huì)選擇 return View()
當(dāng)我需要ajax異步請(qǐng)求到controller做一些事情時(shí),處理結(jié)束后,我會(huì)通過(guò) return Json() 返回操作是否成功或返回的數(shù)據(jù)。
當(dāng)我需要調(diào)用其他action時(shí)我會(huì)使用 RedirectToAction 或 RedirectToRoute。
當(dāng)我什么都不需要做的時(shí)候我會(huì)使用 EmptyResult()。例如數(shù)據(jù)導(dǎo)出操作等。
5.UpdateModel與TryUpdateModel
雖然在MVC 中可以直接在Controller的方法中定義操作對(duì)象,然后通過(guò)表單提交裝入數(shù)據(jù)到對(duì)象,如下:
public ActionResult Register(Account account)
{ //TODO }
但我會(huì)使用UpdateModel和TryUpdateModel去做這些事情,還是簡(jiǎn)單說(shuō)一下這兩個(gè)方法的功能。如果用戶操作的view是完成注冊(cè)用戶功能,此時(shí)表單數(shù)據(jù)已經(jīng)填寫完整,提交表單后轉(zhuǎn)入上面的方法( Register() , 去掉參數(shù)),在方法中使用如下代碼:
public ActionResult Register()
{
????? var account = new Account();
????? UpdateModel(account);
????? //TODO: Register
????? return ……
}
這樣表單中的數(shù)據(jù)可以裝入 account 對(duì)象,后來(lái)發(fā)現(xiàn)有時(shí)總是不成功,程序報(bào)錯(cuò),遂發(fā)現(xiàn)TryUpdateModel(),雖然執(zhí)行后也會(huì)有錯(cuò)誤,但是部分?jǐn)?shù)據(jù)已經(jīng)正確填入程序也并不會(huì)報(bào)錯(cuò),然后將沒(méi)有填入的數(shù)據(jù)(問(wèn)題就發(fā)生在這些數(shù)據(jù)上)單獨(dú)處理。不能裝入的數(shù)據(jù)通常都是由規(guī)律的,例如ID,view中通過(guò)DropDownList選擇的數(shù)據(jù)等等,所以可以使用重載方法將可能會(huì)出錯(cuò)的數(shù)據(jù)排除掉, string[] excludeProperties 參數(shù),這樣TryUpdateModel就不會(huì)報(bào)錯(cuò)了,雖然已經(jīng)找到裝入對(duì)象時(shí)出錯(cuò)的規(guī)律,但還是想找到更準(zhǔn)確的判斷方法,不想這樣偷懶下去。偶然間注意到 ModelState.IsValid 這句經(jīng)常出現(xiàn)的代碼,所以就試了一下,在TryUpdateModel語(yǔ)句執(zhí)行后,在快速監(jiān)視中添加了ModelState,果然可以看到出錯(cuò)的字段(里面有error屬性)。后面的操作就是一樣的了,排除這些字段,單獨(dú)處理!
6.在Action中使用FormCollection,6中使用 string[] excludeProperties 參數(shù)排除掉的字段可以通過(guò)下面的方法賦值
public ActionResult Register(FormCollection form)
{
????? var account = new Account();
????? // city在表單中是下拉框選擇的(DropDownList)
????? if (TryUpdateModel(account , null ,? null , new string[] { "City" } ))?? // UpdateModel也可以重載
????? {
??????????? account.City = form["City"];
????? }
????? return ……
}
轉(zhuǎn)載于:https://blog.51cto.com/huwlnew/622447
總結(jié)
- 上一篇: MySQL Timeout解析
- 下一篇: c++ explicit 修饰构造函数