【ASP.NET】@Model類型的使用詳解


有時需要在ASP.NET MVC4的視圖的@model中使用多個類型的實例,.NET Framework 4.0版本引入的System.Tuple類可以輕松滿足這個需求。

        假設Person和Product是兩個類型,如下是控制器代碼。

[csharp] view plain copy
  1. using System;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace Razor.Controllers  
  5. {  
  6.     public class HomeController : Controller  
  7.     {  
  8.         Razor.Models.Product myProduct = new Models.Product { ProductID = 1, Name = "Book"};  
  9.         Razor.Models.Person myPerson = new Models.Person { PersonID = "1", Name = "Jack" };  
  10.           
  11.         public ActionResult Index()  
  12.         {  
  13.             return View(Tuple.Create(myProduct,myPerson));  // 返回一個Tuple對象,Item1代表Product、Item2代表Person  
  14.         }  
  15.   
  16.     }  
  17. }  

        如下是視圖Index.cshtml的代碼

[html] view plain copy
  1. @model Tuple<Razor.Models.Product, Razor.Models.Person>  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  7.   
  8. <html>  
  9. <head>  
  10.     <meta name="viewport" content="width=device-width" />  
  11.     <title>Index</title>  
  12. </head>  
  13. <body>  
  14.     <div>  
  15.         @Model.Item1.Name  
  16.     </div>  
  17. </body>  
  18. </html>  

        當然,還有許多其它的方法做到上述相同效果。但上述方法直接簡明,容易理解和使用。

 

有時候我們在頁面中會看見@model的語句,都是用於從后台向前端頁面傳遞數據的,下面我們來看看一個案例:

(1)定義Model實體

[csharp] view plain copy
  1. public class SearchWithFundingList  
  2.     {  
  3.         /// <summary>  
  4.         /// 方案分類  
  5.         /// </summary>  
  6.         public int ProjectCategory { get; set; }  
  7.         /// <summary>  
  8.         /// 發起時間小  
  9.         /// </summary>  
  10.         public string MinAddDate { get; set; }  
  11.         /// <summary>  
  12.         /// 發起時間大  
  13.         /// </summary>  
  14.         public string MaxAddDate { get; set; }  
  15.         /// <summary>  
  16.         /// 狀態  
  17.         /// </summary>  
  18.         public int State { get; set; }  
  19.         /// <summary>  
  20.         /// 昵稱  
  21.         /// </summary>  
  22.         public string NickName { get; set; }  
  23.         /// <summary>  
  24.         /// 用戶id  
  25.         /// </summary>  
  26.         public int Mid { get; set; }  
  27.     }  

(2)傳遞Model

[csharp] view plain copy
  1. private SearchWithFundingList GetFormWithFundingNow(int id, int uid)  
  2. {  
  3.     SearchWithFundingList model = new SearchWithFundingList();  
  4.     model.Mid = uid;  
  5.     model.State = WithFundingStateKey.Doing;  
  6.     model.ProjectCategory = id;  
  7.     return model;  
  8. }  

注意:一定要在最后return 實體,不然前台的Model實體是null 

(3)具體調用

在頁面代碼最上面添加上實體的聲明

[html] view plain copy
  1. @{Layout = null;}  
  2. @model StockFunds.Entities.DTO.SearchWithFundingList  

接下來就可以在頁面里使用Model(這里的實體就是指SearchWithFundingList實體),並且此時的Model已經是強類型了,我們可以點出具體的屬性,非常方便

[html] view plain copy
    1. <span class="state">Model.State</span>元</span

 

接下來是一個模擬用戶登錄的表單:

HomeController.cs

namespace Test.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View("~/Views/Home/Login.cshtml");
        }
        public String Login(User user) {
            String name = user.Name;
            String password = user.Password;
            if ("張飛".Equals(name) && "abc".Equals(password))
            {
                return "登錄成功";
            }
            else {
                return "登錄失敗";
            }
        }
    }
}
HomeController.cs

Login.cshtml

@{
    Layout = null;
}
@model Test.Models.User
<html>
    <head>
        <title>用戶登錄</title>
    </head>
    <body>

        <div>
        <form action="Home/Login" method="post">
            用戶名:@Html.TextBoxFor(model=>model.Name)<br/>
            密碼:@Html.TextBoxFor(model=>model.Password)<br />
            <input type="submit" value="提交" />
        </form>
    </div>
    </body>
</html>
Login.cshtml

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM