被模型传至卡了很久,看了以后真心长知识!
七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递
http://www.cnblogs.com/powertoolsteam/p/MVC_two.html
Model类 与 VIewModel
Model 是为了存储数据的数据模型
ModelView是为了视图建立的模型类
Model + Controll + VIew(ViewModel)
就拿新闻系统来说
Model:
为了存储数据:N_New新闻内容 N_catefory 新闻分类 N_Comment 新闻评论
public class N_New
{
[Key]
public int ID { set; get; }
public int CategoryId { get; set; }
public string NewsTitle { get; set; }
public string NewsContent { get; set; }
public DateTime CreateTime { get; set; }
}
public class N_Category
{
[Key]
public int CategoryId { get; set; }
public int CategoryPid { get; set; }
public string CategoryName { get; set; }
}
public class N_Comment
{
[Key]
public int CommentId { get; set; }
public string CommentContent { get; set; }
public DateTime CreateTime { get; set; }
public int NewsId { get; set; }
public int UserId { get; set; }
}
但是新闻有2个界面:1.新闻列表 2.新闻详情
ModelVIew:
public class NewList{//新闻列表
public string New_CategoryName { get; set; }//类型名
public string New_NewTitle { get; set; }//新闻名字
}
public class NewInfor {//新闻详情
public string CategoryName { set; get; }//新闻类型
public N_New N_New { set; get; }//新闻内容
public List<N_Comment> N_Comment { set; get; }//所有评论
}
然后在View调用模型类使用:
@model hycyjyM.Models.New_List
@Model.NewsTitle <p/>
@Model.New_CategoryName
PS:刚开始学
在C : return View( );卡了好久
其实这里传过来的模型在视图层调用就是 @Model.????
那上面的类来说:
C:
NewList nl = new NewList();
return VIew(nl);
V:
@model hycyjyM.Models.New_List //先引用
@Model.NewsTitle <p/> //通过@Model使用值
@Model.New_CategoryName