| 小弟刚学MVC3,在学习过程中遇到了很多的问题,现在把已遇到问题总结出来,以后陆续更新。方便和我一样的新手。。 1.手写Model类,EF执行错误找不到表对象。  
 [TableAttribute("ProductEntity")]public class ProductEntity{}
 2.加载不同的Layout,在_ViewStart.cshtml中添加逻辑  
 @{if (Request.Url.AbsoluteUri.Contains("Manage")){
 Layout = "~/Views/Shared/_MLayout.cshtml";
 }else{
 Layout = "~/Views/Shared/_LayoutLogin.cshtml";
 }
 }
 3.图片image设置动态urla.Detail/Creat/Edit页面:
  
 @model TaiQiu.Models.ProductEntity<img id="preview" src="@Html.DisplayFor(model => model.PicUrl)"/>
 b.List页面:  
 @model IEnumerable<TaiQiu.Models.ProductEntity>   @foreach (var item in Model)
 {
 <img src="@item.PicUrl" alt="@item.Title"/>
 }
 4.用户登录/权限  
 //验证用户成功后,将用户写入cookieSystem.Web.Security.FormsAuthentication.SetAuthCookie(_user, false);
 //后台Controller中添加Authorize,如果可以配置Users/Role
 [Authorize(Users/Role = 允许账号/角色)]
 public class ManageController : Controller{}
 配置文件中其中Form验证  
    <authentication mode="Forms"><forms loginUrl="~/Login/" timeout="2880" />
 </authentication>
 5.IIS6配置MVC3  
 找不到 System.Web.Razor,System.Web.MVC 等。需要把开发环境下对应的dll复制到服务器bin文件下 6.View中控件样式设置  
 @Html.TextAreaFor(model => model.Infor, new { style = "width:800px;height:400px" })或者
 @Html.TextAreaFor(model => model.Infor, new { @class=样式名})
 7.TextArea设置Rows,Columns(第2个参数为rows,第3个参数为columns)  
 @Html.TextAreaFor(model => model.FileInfo,5,5, new { style="width:300px;height:100px;"}) 8.文件上传,注意加粗红色部分 View代码:  
 @using (Html.BeginForm("actionName", "cotrollerName", FormMethod.Post, new { enctype = "multipart/form-data" })){
 <input type="file" name="FilePath" id="FilePath" />
 }
 Controller代码:  
 HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase;if (file.FileName != "")
 {
 //code
 }
 9.foreach,使用ViewBag动态特性 Controller代码:  
 var recommendporduct = PE.ProductEntity.Where(a => a.Recommend).Take(5);ViewBag.rplist = recommendporduct;
 return View();
 View代码:(注意:使用ViewBag,不会有代码提示。)  
        @foreach (var item in ViewBag.rplist){
 <div class="rught_cptxt2">
 <ul>
 <li class="rught_cpimg1"><a href="#">
 <img src="@item.PicUrl.Trim()" border="0" /></a></li>
 <li class="rught_cptxt1"><a href="#">@item.Title</a></li>
 </ul>
 </div>
 }
 10.DropDownList绑定Controller代码:
  
         //类别public SelectList GetSL()
 {//此处静态数据,可以使用EF读取数据库
 List<SelectListItem> list = new List<SelectListItem> {
 new SelectListItem(){Value="0",Text="新闻资讯"},
 new SelectListItem(){Value="1",Text="技术文章"}
 };
 return new SelectList(list, "Value", "Text");
 }
 public ActionResult Create()
 {
 ViewBag.ddl = GetSL();
 return View();
 }
 View代码:  
 //引号中对应ViewBag中的值@Html.DropDownList("ddl")
 或者使用强类型(将GetSL()静态方法放在类中,直接调用。) @Html.DropDownListFor(model => model.IsTechnology, GetSL(), "请选择")  11.查询 a.EF4.1中支持使用sql: DbSet<BlogMaster>set= context.Set<BlogMaster>();List<BlogMaster> list =set.SqlQuery("select *from BlogMaster where UserId='3'").ToList();
 b.使用linq to entity framework:DemoDBEntities context =new DemoDBEntities();
 DbSet<BlogMaster>set= context.Set<BlogMaster>();
 var result = from u inset.ToList()
 where u.UserID ==3
 select u;
 c.使用labmda查询:var list =set.Where(o => o.UserID ==3);
 var listGroup =set.GroupBy(o => o.UserID);
 12.输出html标签      13.@使用  
 输出@符号:@@输出Email地址:Razor模板会自动识别出Email地址,所以不需要我们进行任何的转换。而在代码块中,只需要使用 @:Tom@gmail.com 即可。@:表示后面的内容为文本。输出HTML代码(包含标签):直接输出,string html = "<font color='red'>文本</font>"; @html输出HTML内容(不包含标签):有两种方法,第一种:IHtmlString html=new HtmlString("<font color='red'>文本</font>"); @html;
 第二种:string html = "<font color='red'>文本</font>"; @Html.Raw(html);
     未完。。待续。。。。
 
 
 
 
 |