MVC小技巧
1.創(chuàng)建控件,MVC中通過htmlHelper生成HTML標簽。
1 <%= Html.TextBox("UserName")%>
2 <%= Html.TextBox("UserName","Coolin")%>
3 <%= Html.TextBox("UserName","Coolin",
??????????? new { @class = "className",disabled = true })%>
最后一項htmlAttributes,可以設(shè)置樣式等屬性。
2.RegisterRoutes帶來了什么變化。通過VS創(chuàng)建一個MVC應(yīng)用程序,會在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)在看一下它帶來的變化,假設(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" }) %>
對應(yīng)的Url應(yīng)該是? localhost:XXXX/Account/List/Man 和 localhost:XXXX/Account/List/Woman
當然也可以不在Global.asax中注冊那條規(guī)則,對應(yīng)的Url就會變成大家熟悉的 localhost:XXXX/Account/List?type=Man。
順便提一下我在開發(fā)中遇到過的一個問題,還以上面的例子為例,在程序的開發(fā)階段,沒有加入剛才那條規(guī)則,當我們在程序中加入了 sitemap(mvcsitemap),結(jié)果卻是sitemap的路徑永遠不能指向Woman的路徑,可能是sitemap不能通過 ?后面參數(shù)區(qū)分路徑,后來加入了這條規(guī)則,url中去掉參數(shù),問題解決了。
3.ajax異步請求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???????????? //通過腳本移除此行
5???????????? alert("刪除成功!")???
6???????? }
7???? });
?
4.在view與Mvc view user control中使用強類型。
<%@ 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);
}
順便一提:
當我需要呈現(xiàn)View的時候我會選擇 return View()
當我需要ajax異步請求到controller做一些事情時,處理結(jié)束后,我會通過 return Json() 返回操作是否成功或返回的數(shù)據(jù)。
當我需要調(diào)用其他action時我會使用 RedirectToAction 或 RedirectToRoute。
當我什么都不需要做的時候我會使用 EmptyResult()。例如數(shù)據(jù)導出操作等。
5.UpdateModel與TryUpdateModel
雖然在MVC 中可以直接在Controller的方法中定義操作對象,然后通過表單提交裝入數(shù)據(jù)到對象,如下:
public ActionResult Register(Account account)
{ //TODO }
但我會使用UpdateModel和TryUpdateModel去做這些事情,還是簡單說一下這兩個方法的功能。如果用戶操作的view是完成注冊用戶功能,此時表單數(shù)據(jù)已經(jīng)填寫完整,提交表單后轉(zhuǎn)入上面的方法( Register() , 去掉參數(shù)),在方法中使用如下代碼:
public ActionResult Register()
{
????? var account = new Account();
????? UpdateModel(account);
????? //TODO: Register
????? return ……
}
這樣表單中的數(shù)據(jù)可以裝入 account 對象,后來發(fā)現(xiàn)有時總是不成功,程序報錯,遂發(fā)現(xiàn)TryUpdateModel(),雖然執(zhí)行后也會有錯誤,但是部分數(shù)據(jù)已經(jīng)正確填入程序也并不會報錯,然后將沒有填入的數(shù)據(jù)(問題就發(fā)生在這些數(shù)據(jù)上)單獨處理。不能裝入的數(shù)據(jù)通常都是由規(guī)律的,例如ID,view中通過DropDownList選擇的數(shù)據(jù)等等,所以可以使用重載方法將可能會出錯的數(shù)據(jù)排除掉, string[] excludeProperties 參數(shù),這樣TryUpdateModel就不會報錯了,雖然已經(jīng)找到裝入對象時出錯的規(guī)律,但還是想找到更準確的判斷方法,不想這樣偷懶下去。偶然間注意到 ModelState.IsValid 這句經(jīng)常出現(xiàn)的代碼,所以就試了一下,在TryUpdateModel語句執(zhí)行后,在快速監(jiān)視中添加了ModelState,果然可以看到出錯的字段(里面有error屬性)。后面的操作就是一樣的了,排除這些字段,單獨處理!
6.在Action中使用FormCollection,6中使用 string[] excludeProperties 參數(shù)排除掉的字段可以通過下面的方法賦值
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解析
- 下一篇: 如何修改Ubuntu Linux的时间