首页 新闻 会员 周边 捐助

在mvc中怎么创建ViewModel?

0
悬赏园豆:10 [已解决问题] 解决于 2014-06-25 15:50

在mvc中怎么创建ViewModel?
每次创建完都变成了Model的实体,而不是ViewModel,

jason2013的主页 jason2013 | 小虾三级 | 园豆:1998
提问于:2014-06-24 18:58
< >
分享
最佳答案
0

ViewModel就是一个实体类,定义上跟Model没有任何区别.只是在设计和使用时要依赖页面

收获园豆:10
吴瑞祥 | 高人七级 |园豆:29449 | 2014-06-25 09:59
其他回答(2)
0

新建一个类,按照ViewModel的写法去写,不就ok了?

幻天芒 | 园豆:37207 (高人七级) | 2014-06-24 19:20
-1

基本上是按照DB中的字段去写,ViewModel就是对应View中的数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using HTCSystem.Data.Translations;
namespace HTC_Development_Template1Portal.ViewModels
{
    public class StoreModelView
    {
        [Display(Name = "StoreID")]
        public int StoreId { get; set; }

        [Display(Name = "StoreName", ResourceType = typeof(UserInterfaceResx))]
        public string StoreName { get; set; }

        [Display(Name = "Country", ResourceType = typeof(UserInterfaceResx))]
        public string CountryDigit2 { get; set; }

        [Display(Name = "Area", ResourceType = typeof(UserInterfaceResx))]
        public int AreaId { get; set; }

        [Display(Name = "State", ResourceType = typeof(UserInterfaceResx))]
        public int ProvinceId { get; set; }

        [Display(Name = "City", ResourceType = typeof(UserInterfaceResx))]
        public int CityId { get; set; }

        [Display(Name = "Tel", ResourceType = typeof(UserInterfaceResx))]
        public string Tel { get; set; }

        [Display(Name = "Fax", ResourceType = typeof(UserInterfaceResx))]
        public string Fax { get; set; }

        [Display(Name = "Phone", ResourceType = typeof(UserInterfaceResx))]
        public string Phone { get; set; }

        [Display(Name = "Contact", ResourceType = typeof(UserInterfaceResx))]
        public string Contact { get; set; }

        [Display(Name = "Email", ResourceType = typeof(UserInterfaceResx))]
        public string Email { get; set; }

        [Display(Name = "Address", ResourceType = typeof(UserInterfaceResx))]
        public string Address { get; set; }

        [Display(Name = "StoreGroup", ResourceType = typeof(UserInterfaceResx))]
        public int StoreGroupId { get; set; }

        [Display(Name = "HTCSales", ResourceType = typeof(UserInterfaceResx))]
        public int? SalesId { get; set; }

        [Display(Name = "KeyWord", ResourceType = typeof(UserInterfaceResx))]
        public string KeyWord { get; set; }

        [Display(Name = "BusinessCircle", ResourceType = typeof(UserInterfaceResx))]
        public string BusinessCircle { get; set; }

        [Display(Name = "HTCAmbassador", ResourceType = typeof(UserInterfaceResx))]
        public int? HTCAmbassadorId { get; set; }

        [Display(Name = "StoreLevel", ResourceType = typeof(UserInterfaceResx))]
        public string StoreLevel { get; set; }
    }
}
View Code

View中 调用对应的ViewModel:

@model 项目名称.ViewModels.StoreModelView

 

sirili | 园豆:44 (初学一级) | 2014-06-25 11:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册