【原創】ASP.NET MVC3開發中遇到問題以及解決方法


小弟剛學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.Raw(內容)

 

 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);

 

 

未完。。待續。。。。





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM