在剛學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設定動態url
a.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.使用者登入/許可權
//驗證使用者成功後,將使用者寫入cookie
System.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標籤
傳參
@Html.Action("Header","Common",new RouteValueDictionary{{"controllerName",controllerName }})
@Html.Raw(內容)