**Model**```csharp public class ExhibitionList { [Required] [MaxLength(50)] public string ExhibitionName { get; set; }
public DateTime CreateDate { get; set; }
public DateTime EditDate { get; set; } }
Controller```csharp public ActionResult ExhibitionList() { var tuple = new Tuple<IEnumerable, ExhibitionList>(Repository.GetExhibitionList(), new ExhibitionList()); return PartialView(tuple); }
第二種方式就是建立一個ViewModel的方式 Model```csharp /// 展覽列表的ViewModel public class ExhibitionListViewModel { public IEnumerable IEmumerable_ExhibitionList { get; set; } public ExhibitionList ExhibitionListModel { get; set; } }
1 2 3 4 5 6 7 8 9
**View**```csharp public ActionResult ExhibitionList() { ExhibitionListViewModel model = new ExhibitionListViewModel(); model.IEmumerable_ExhibitionList = Repository.GetExhibitionList(); model.ExhibitionListModel = new ExhibitionList(); return PartialView(model); }
首先先建立一個可以將分隔符號拆開,並且去搜尋對應名稱的function (抓網路上高手寫的Code回來修改而成) ```sql create FUNCTION [dbo].[ufn_Split]( @InputString nvarchar(4000)) RETURNS varchar(1000) –執行這個function會回傳字串 AS BEGIN –用來儲存分隔符號的Index DECLARE @CIndex smallint –用來串接字串並最後回傳回去的字串 declare @OutputString varchar(1000) =’’ declare @tempString varchar(500) =’’
WHILE (@InputString<>’’) BEGIN SET @CIndex=CHARINDEX(‘,’,@InputString) –抓到第一個分隔符號的Index IF @CIndex=0 SET @CIndex=LEN(@InputString)+1
–將第一組代碼拆解出來並帶到T2去搜尋對應的名稱,如果搜尋出來為null的話則當作空字串處理 set @tempString = isnull((select name from t2 where id = (SUBSTRING(@InputString,1,@CIndex-1))),’’)
–這串if else純粹只是要解決多餘的逗號問題而已,就不多加解釋了 if @tempString <> ‘’ begin if @OutputString = ‘’ begin set @OutputString = @tempString end else set @OutputString = @OutputString +’,‘+@tempString end
IF @CIndex=LEN(@InputString)+1 BREAK
–把剛剛搜尋過的代碼挑掉後,剩下的繼續跑迴圈 SET @InputString=SUBSTRING(@InputString,@CIndex+1,LEN(@InputString)-@CIndex) END RETURN @OutputString END
1 2 3 4
最後SQL command如下,就可以得到上述合併的結果了!!! ```sql select title,dbo.ufn_Split(number) from t1
[Authorize]//預設這個controller都要驗證過後才能讀取 public class ExhibitionController : Controller { [AllowAnonymous]//這個action無需驗證,所有人皆能直接讀取 public ActionResult Index() {
return View();
} public ActionResult about() {
return View();
} }
</pre>
驗證成功後呼叫FormsAuthentication.RedirectFromLoginPage(),告訴網站該使用者已經通過驗證。 ```csharp if (Acclogin.CheckPassword()) { FormsAuthentication.RedirectFromLoginPage(Acclogin.Account, false); return Redirect(returnUrl); }
create procedure sp_CheckLoginPassword
@Account varchar(50),
@Password varchar(150)
as
declare @validate int
set @validate = (
select PWDCOMPARE(@Password,tcuser.Password)
from tcuser
where tcuser.Account = @Account) select isnull(@validate,0)
* 建立Entity Framwork,將剛剛寫的StoreProcedure更新進來
透過Entity Framwork建立出來的物件呼叫該方法就大功告成了!!
public bool CheckPassword() { //傳回0表示驗證失敗; 1表示成功 NTIEntities db = new NTIEntities(); bool isValidate = db.sp_CheckLoginPassword(this.Account, this.Password).First() == 1 ? true : false; return isValidate; }
驗證方法成功後呼叫FormsAuthentication.RedirectFromLoginPage(),告訴網站該使用者已經成功登入 ```csharp if (Acclogin.CheckPassword()) { FormsAuthentication.RedirectFromLoginPage(Acclogin.Account, false); return Redirect(returnUrl); }