zoukankan      html  css  js  c++  java
  • ASP.NET MVC Controller向View传值的几种方式

    1)ViewBag

      ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.myName

        控制器代码:

    1 public ActionResult Index()  
    2 {  
    3     ViewBag.name = "梁顺盛";  
    4     ViewBag.message = "欢迎使用MVC设计模式~~";  
    5     return View();  
    6 }  

    视图代码:

    <div>          
        <!--利用HtmlHelper创建TextBox时,使用名称与ViewBag中的Key一致, 就会自动实现值绑定-->  
        @Html.TextBox("name")  
        @ViewBag.message          
    </div> 

    2)ViewData

         ViewData只对当前Action有效,它是一个字典集合,通过key值读取对应的value;

         控制器代码:

    public ActionResult Index()  
            {  
                ViewData["name"] = "梁顺盛";  
                ViewData["message"] = "欢迎使用MVC设计模式~~";  
                return View();  
            }  

    视图代码:

    <div>          
            <!--利用HtmlHelper创建TextBox时,使用名称与ViewData中的Key一致, 就会自动实现值绑定-->  
            @Html.TextBox("name")  
            @ViewData["message"]          
        </div> 

    3)TempData

        使用TempData和使用ViewData方法是一样的,但是它可用于在不同的Action之间传值,这是ViewData做不到的。

       控制器代码:

    public class MVCController : Controller  
    {     
        public ActionResult Index1()  
        {  
            TempData["name"] = "梁顺盛";              
            return View();  
        }  
      
        public ActionResult Index2()  
        {  
            string strName = TempData["name"].ToString()  
            return View();  
        }  

    上面的代码中,Index()给TempData添加了一个键值对,假设我们先请求Index这个Action,接着请求Index2这个Action,那么在Index2中,我们便可以得到之前添加到TempData的键值对。有趣的是,这时如果再次请求Index2,那么从TempData中读到的MyName的值会是null。这是因为TempData和一个临时的Session差不多,当Acion执行的时候它做为一个全局对象保存的内存中,而一旦Action的执行完成,就会释放内存空间,这就是它与ViewData最大的不同之处。

    4)Model

         Controller通过Model传值应该是MVC中使用最为普遍的一种传值方式,通过强类型绑定,在View中可以很方便的通过Model的相应属性得到想要的值。

         Model代码:

    public partial class YzAdministratorEntity  
    {  
        public YzAdministratorEntity()  
        {  
            this.YzClearRecordEntity = new HashSet<YzClearRecordEntity>();  
            this.YzNotifyInfoEntity = new HashSet<YzNotifyInfoEntity>();  
        }  
      
          
        public System.Guid ID { get; set; }  
        public string AdminPassword { get; set; }  
        public string AdminName { get; set; }  
        public bool isUsed { get; set; }  
      
        [Required(AllowEmptyStrings = false, ErrorMessage = "用户ID不能为空")]  
        public string AdministratorID { get; set; }  
        public string AdminLevel { get; set; }  
      
        public virtual ICollection<YzClearRecordEntity> YzClearRecordEntity { get; set; }  
        public virtual ICollection<YzNotifyInfoEntity> YzNotifyInfoEntity { get; set; }  
    }  

    视图代码:

    强类型绑定:

    @model Model.YzAdministratorEntity

    通过Linq自动匹配Model和Label或TxtBox的属性值:

    <div>  
        <span class="editor-label">  
            @Html.Label("编号:")  
        </span>  
        <span class="editor-field">  
            @Html.EditorFor(a => a.AdministratorID)  
            @Html.ValidationMessageFor(model => model.AdministratorID)  
        </span>  
    </div>  
      
    <div>  
        <span class="editor-label">  
            @Html.Label("密码:")  
        </span>  
        <span class="editor-field">  
            @Html.EditorFor(a => a.AdminPassword )  
        </span>  
    </div>  
      
    <div>  
        <span class="editor-label">  
            @Html.Label("真实姓名:")  
        </span>  
        <span class="editor-field">  
            @Html.EditorFor(a => a.AdminName )  
        </span>  
    </div>  
  • 相关阅读:
    测试必备基础知识总结
    初级测试工程师与高级测试工程师的区别
    调试Java程序持续占cpu问题
    Spring Struts Hibernate trouble shooting | 一些问题的记载
    MySQL 性能调优之查询优化
    Tomcat性能调优 | Tomcat Performance Tuning
    Tomcat配置、管理和问题解决 | Tomcat Configuration, Manangement and Trouble Shooting
    SWT Browser & XULRunner
    Windows应用安装制作工具调查报告
    CentOS Configuration | CentOS配置
  • 原文地址:https://www.cnblogs.com/liangss/p/5122503.html
Copyright © 2011-2022 走看看