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