今天要用到昨天設計的流程,還有昨天講的
Int.Parse(“string”)、int.ToString()、string.Split()、HttpContext.Session.GetString()
以及第五天提到的c#中的LinQ語法。
下面來看看我寫的程式碼(only 部分)
.cshtml中table跟下拉式選單的程式碼,input type = hidden,可以讓資料不在前端顯示,當表單送出時一起送出,這邊我使用input type = hidden將上一頁輸入的座位表行列數再送出去一次。
<table id="dtable">
<form asp-controller="newSeat" asp-action="newSeat2" method="POST">
<input type="hidden" name="TID" value= @ViewBag.TID >
<input type="hidden" name="C" value= @ViewBag.C >
<input type="hidden" name="R" value= @ViewBag.R >
@{int C = Int32.Parse(ViewBag.C);
int R = Int32.Parse(ViewBag.R);
for(int i = 0; i < R ;i ++){
<tr>
@for(int j = 0; j < C ;j ++){
<td>
<select name="seat">
<option value="AISLE">走道</option>
<option value="on" selected>座位</option>
</select>
</td>
}
</tr> }
}<input name="submit" value="Send" type="submit">
<p> </p>
<input name="submit" value="Reset Table" type="submit">
</form>
</table>
下面是最主要的action funtion程式碼,我新增了一個controller(newSeat.cs),來專門處理建立座位表的相關事宜,這邊寫來再次提醒自己,asp-controller、asp-action、 function還有view的關係還有路徑請注意,做一做常常眼睛疲勞,很容易對錯,整個流程都不對了,下面用到第五天說的post方法及新增查詢資料庫方法,還有第六天說的session。
[HttpPost]
public ActionResult newSeat2(IFormCollection post){
ViewBag.UID = HttpContext.Session.GetString("UID");
if(ViewBag.UID == null || string.IsNullOrWhiteSpace(ViewBag.UID.ToString())){
return View("Views/shome/Login.cshtml");
}else{
string submit = post["submit"];
if(submit == "Reset Table"){
return View("Views/newSeat/newSeat1.cshtml");
}else{
string UID = ViewBag.UID.ToString();
string TNAME = post["TID"];
int TID;
string seat= post["seat"];
string C = post["C"];
string R = post["R"];
//ArrayList seat_statues = new ArrayList();
string[] seat_statues = seat.Split(",");
foreach (string temp in seat_statues)
//Console.WriteLine(temp);
ViewBag.TID = TNAME;
using (var ctx = new ithelp12Context()){
var A = ctx.TableUser.Where(s => s.UName == UID).FirstOrDefault();
//Console.WriteLine(A.UId);
var TALL = new TableAll { TName = TNAME, TAdminId = A.UId, TMemder = A.UId.ToString(),TOpen = "true",TStop = "false"};
ctx. TableAll.Add(TALL);
ctx.SaveChanges();
}
using (var ctx = new ithelp12Context()){
var T = ctx.TableAll.Where(s => s.TName == TNAME).FirstOrDefault();
//Console.WriteLine(T.TId);
TID = T.TId;
var A = new TableStatus { TId = TID, TStatusName = "座位", TStatusColor = "#336666"};
var B = new TableStatus { TId = TID, TStatusName = "走道", TStatusColor = "#ffffff"};
ctx. TableStatus.Add(A);
ctx. TableStatus.Add(B);
ctx.SaveChanges();
}
using (var ctx = new ithelp12Context()){
int c_seat=0;
var S1 = ctx.TableStatus.Where(s => s.TId == TID && s.TStatusName =="座位").FirstOrDefault();
var S2 = ctx.TableStatus.Where(s => s.TId == TID && s.TStatusName =="走道").FirstOrDefault();
//Console.WriteLine(S1.TStatusName + " " + S2.TStatusName);
for(int i=0; i < Int32.Parse(R) ;i++){
for(int j=0; j < Int32.Parse(C) ;j++){
if(seat_statues[c_seat] == "on"){
var A = new TableDetail() {TId = TID, TX = j+1, TY = i+1,SName = "座位",TStatusId=S1.TStatusId};
ctx.TableDetail.Add(A);
}else{
var A = new TableDetail() {TId = TID, TX = j+1, TY = i+1,SName ="走道",TStatusId=S2.TStatusId};
ctx.TableDetail.Add(A);
}
c_seat +=1 ;
}
}
ctx.SaveChanges();
}
//Console.Write(seat);
ViewBag.mark = "newSeat2";
return View("Views/shome/user_home.cshtml");
}
}
}
新增座位表功能實作成果
DAY8心得:
可怕啊可怕,週末竟然沒有超前進度留存搞,我這週末還要去新竹找朋友玩,該怎麼辦才好啊我作功能做的很開心,但好像沒甚麼特別的技術,明天可能穿插介紹一點別的東西。