zoukankan      html  css  js  c++  java
  • 【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的代码

    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)具体调用

    在页面代码最上面添加上实体的声明

    1. @{Layout = null;}  
    2. @model StockFunds.Entities.DTO.SearchWithFundingList  

    接下来就可以在页面里使用Model(这里的实体就是指SearchWithFundingList实体),并且此时的Model已经是强类型了,我们可以点出具体的属性,非常方便

      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
  • 相关阅读:
    主成分分析法(PCA)答疑
    搜索引擎的高级用法
    Makefile 编写实例
    GCC常用命令
    一个进程最多能开多少个线程?
    归并排序
    选择排序(数组、链表)
    求连续子数组的最大和
    生产者-消费者问题(1)
    基于cmake编译安装MySQL-5.5
  • 原文地址:https://www.cnblogs.com/HDK2016/p/7777362.html
Copyright © 2011-2022 走看看