項目要求
練習1:
@RPHelper.CheckBoxList(Model.Persons, "Id", "Name", Model.PersonId)
<input type="checkbox" name="managerId" value="1"/><label>rupeng</lable> <br/>
<input type="checkbox" name="managerId" value="2" checked/><label>yzk</lable> <br/>
是項目 ProjectLX
第一步:寫類CsHtmlHelper.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using RazorEngine;
using RazorEngine.Text;
using System.Text;
using System.Reflection;
using System.Collections;
namespace ProjectsLX
{
public class CsHtmlHelper
{
/// <summary>
/// 讀取模板文件,並給模板文件加一個帶修改時間的別名字
/// </summary>
/// <param name="context">方便調用context中的方法</param>
/// <param name="csHtmlVirtuPath">模板的虛擬路徑,方便取別名</param>
/// <param name="model">方便傳值</param>
/// <returns></returns>
public static string ParseRazor(HttpContext context, String csHtmlVirtuPath, Object model)
{
string fullpath = context.Server.MapPath(csHtmlVirtuPath);
string cshtml = File.ReadAllText(fullpath);
string cacheName = fullpath + File.GetLastWriteTime(fullpath);
string html = Razor.Parse(cshtml, model, cacheName);//模板,model,別名
return html;
}
public static RawString CheckBoxList(IEnumerable items,
string valuePropName, string textPropName, object selectedValue, object extendProPerties)
{
StringBuilder sb = new StringBuilder();
//利用反射Type調用程序集中的匿名類的 名字
Type extendPropertiesType = extendProPerties.GetType();
PropertyInfo[] extPropInfos = extendPropertiesType.GetProperties();//反射獲取類中的屬性
//2.2拼接出來<label>標籤
foreach (object item in items)//items是傳遞過來的list集合,,item是集合中的一個類
{
//1.1拼接出<input >
sb.Append("<input ");
//1---.1拿到<input type="checkbox" name="manager" style=... />中的type="checkbox" name="manager" style=... 屬性
foreach (var extPropInfo in extPropInfos)
{
string extProName = extPropInfo.Name;//獲取屬性的名字
object extProValue = extPropInfo.GetValue(extendProPerties);//獲取匿名類extendProPerties中該屬性extPropInfo的值
sb.Append(" ").Append(extProName).Append("='").Append(extProValue).Append("'");
}
//1---.2拿到<input type="checkbox" name="manager" style=... />中的 value="1" 屬性
Type itemType = item.GetType(); //獲取item的類名字
PropertyInfo valuePropInfo = itemType.GetProperty(valuePropName);//獲取item的類中指定屬性的 <名字>;valuePropName用來指定
object itemValue = valuePropInfo.GetValue(item);//獲得item表示的類中指定屬性“Id”的<值>
sb.Append(" ").Append("value").Append("=").Append(itemValue.ToString()).Append("");
//1---.3如果是選中的就加上 "checked"
if (object.Equals(itemValue, selectedValue))
{
sb.Append(" ").Append("checked");//等於< .... checked />屬性,它被選中
}
sb.Append(" ").Append("/>");
//2.2....
PropertyInfo textPropInfo = itemType.GetProperty(textPropName);////獲取item的類中指定屬性的 <名字>;textPropName用來指定
object itemTextValue = textPropInfo.GetValue(item);//獲得item表示的類中指定屬性“Name”的<值>
sb.Append("<label >").Append(itemTextValue).Append("</label>");
sb.Append("<br />");
}
return new RawString(sb.ToString());
}
}
}
第二步:寫模板RazorCheckBox.cshtml
<!--1.1首先在模板文件中讀取CsHtmlHelper的命名空間-->
@using ProjectsLX
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<!--調用封裝的類CsHtmlHelper中CheckBoxList方法,生成多選框-->
@CsHtmlHelper.CheckBoxList(Model.Persons, "Id", "Name", Model.PersonId, new {type="checkbox",name="manager",style="color:red"});
</body>
</html>
第三步:寫類Persons.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectsLX
{
public class Persons
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
第四步:寫RazorCheckBox.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectsLX
{
/// <summary>
/// RazorCheckBox 的摘要說明
/// </summary>
public class RazorCheckBox : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
List<Persons> list = new List<Persons>();
list.Add(new Persons { Id = 1, Name = "雷軍", Age = 30 });
list.Add(new Persons { Id = 2, Name = "馬化騰", Age = 32 });
list.Add(new Persons { Id = 3, Name = "李彥宏", Age = 31 });
list.Add(new Persons { Id = 4, Name = "xcl", Age = 3 });
list.Add(new Persons { Id = 5, Name = "李彥宏", Age = 12 });
list.Add(new Persons { Id = 6, Name = "聯想", Age = 34 });
list.Add(new Persons { Id = 7, Name = "騰訊", Age = 31 });
list.Add(new Persons { Id = 8, Name = "百度", Age = 38 });
list.Add(new Persons { Id = 9, Name = "泡泡", Age = 151 });
//匿名類中傳遞參數
string html = CsHtmlHelper.ParseRazor(context, "~/RazorCheckBox.cshtml", new {Persons=list,PersonId=3, });
//3.將轉換過的模板頁內容輸入到瀏覽器中
context.Response.Write(html);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
運行結果
總結
1.cshtml模板c#語句後邊不要加分號 “ ; ”
2.cshtml文件導入命名空間,命名空間看HtmlHelper的命名空間來確定,結尾也是不添加 分號的 ;
3,cshtml調用方法 @HtmlHelper.OutHtml(context,”~/1.html”);顯示有編譯錯誤,但是F6之後,發現沒有錯誤了又;