返回筆記列表
/ 筆記詳情
以下是你提供程式碼及需求內容,整理成清楚的 Markdown 格式,整合包含:
- 字典變數與 List 字典定義
- 強型別類別 Message 定義
- Controller 新舊資料傳遞
- Razor Index 檢視頁面顯示
1. 定義字典變數範例¶
// 單筆字典資料
Dictionary<string, dynamic> message = new Dictionary<string, dynamic> {
{"id",1 }, {"pdate",DateTime.Now}, {"content","資管三乙"}
};
ViewBag.message = message;
return View();
2. Index檢視畫面單筆資料顯示¶
@{
Dictionary<string, dynamic> message = ViewBag.message as Dictionary<string, dynamic>;
}
<div class="container overflow-scroll" style="max-height:400px">
<table class="table min-vw-80">
<thead>
<tr>
<th class="w-25">日期</th>
<th class="w-50">內容</th>
<th>動作</th>
</tr>
</thead>
<tbody>
<form action="~/Home/Index" method="post">
<tr>
<td>@message["pdate"].ToString("yy-MM-dd, HH:mm")</td>
<td>@message["content"]</td>
<td>
<button class="btn btn-success" type="submit" name="btn" value="2">修改</button>
<button class="btn btn-danger" type="submit" name="btn" value="3">刪除</button>
</td>
</tr>
<input type="hidden" name="" value='@message["id"]' />
</form>
</tbody>
</table>
</div>
3. 包含三筆字典資料 List 定義並傳值¶
List<Dictionary<string, dynamic>> messages = new List<Dictionary<string, dynamic>>
{
new Dictionary<string, dynamic> {
{"id", 1}, {"pdate", DateTime.Now}, {"content", "資管三甲"}
},
new Dictionary<string, dynamic> {
{"id", 2}, {"pdate", DateTime.Now}, {"content", "資管三乙"}
},
new Dictionary<string, dynamic> {
{"id", 3}, {"pdate", DateTime.Now}, {"content", "資管三丙"}
}
};
ViewBag.messages = messages;
return View();
4. Index檢視畫面多筆資料顯示(List)¶
@{
List<Dictionary<string, dynamic>> messages = ViewBag.messages as List<Dictionary<string, dynamic>>;
}
<div class="container overflow-scroll" style="max-height:400px">
<table class="table min-vw-80">
<thead>
<tr>
<th class="w-25">日期</th>
<th class="w-50">內容</th>
<th>動作</th>
</tr>
</thead>
<tbody>
@foreach (Dictionary<string, dynamic> m in messages)
{
<form action="~/Home/Index" method="post">
<tr>
<td>@m["pdate"].ToString("yy-MM-dd, HH:mm")</td>
<td>@m["content"]</td>
<td>
<button class="btn btn-success" type="submit" name="btn" value="2">修改</button>
<button class="btn btn-danger" type="submit" name="btn" value="3">刪除</button>
</td>
</tr>
<input type="hidden" name="" value='@m["id"]' />
</form>
}
</tbody>
</table>
</div>
5. 定義 Message 類別(Model 資料類別)¶
public class Message
{
public int Id { get; set; }
public DateTime Pdate { get; set; }
public string Content { get; set; }
public Message(int i, DateTime p, string c)
{
this.Id = i;
this.Pdate = p;
this.Content = c;
}
}
6. Controller 同時使用 Dictionary 與 Message 物件¶
public IActionResult Index(int id, int btn)
{
Dictionary<string, dynamic> message = new Dictionary<string, dynamic> {
{"id", 1 }, {"pdate", DateTime.Now}, {"content", "資管三乙"}
};
Message _message = new Message(1, DateTime.Now, "資管三乙");
List<Dictionary<string, dynamic>> messages = new List<Dictionary<string, dynamic>>
{
new Dictionary<string, dynamic> {
{"id", 1 }, {"pdate", DateTime.Now}, {"content", "資管三甲"}
},
new Dictionary<string, dynamic> {
{"id", 2 }, {"pdate", DateTime.Now}, {"content", "資管三乙"}
},
new Dictionary<string, dynamic> {
{"id", 3 }, {"pdate", DateTime.Now}, {"content", "資管三丙"}
}
};
List<Message> _messages = new List<Message>
{
new Message(1, DateTime.Now, "資管三甲"),
new Message(2, DateTime.Now, "資管三乙"),
new Message(3, DateTime.Now, "資管三丙")
};
ViewBag.messages = messages;
return View(_messages);
}
7. Index檢視畫面,使用強型別 Model 顯示多筆 Message¶
@model List<WebApplication1.Models.Message>
<div class="container overflow-scroll" style="max-height:400px">
<table class="table min-vw-80">
<thead>
<tr>
<th class="w-25">日期</th>
<th class="w-50">內容</th>
<th>動作</th>
</tr>
</thead>
<tbody>
@foreach (Message m in Model) {
<form action="~/Home/Index" method="post">
<tr>
<td>@m.Pdate.ToString("yy-MM-dd, HH:mm")</td>
<td>@m.Content</td>
<td>
<button class="btn btn-success" type="submit" name="btn" value="2">修改</button>
<button class="btn btn-danger" type="submit" name="btn" value="3">刪除</button>
</td>
</tr>
<input type="hidden" name="id" value='@m.Id' />
</form>
}
</tbody>
</table>
</div>
這份 Markdown 內容幫你整理完整,涵蓋字典、強型別 Model、多筆資料的 Controller 與 View 表示方式,方便你直接參考與複製使用。
附件 127
根目錄
(6)
wwwroot
(1)
lib
(0)
jquery-validation-unobtrusive
(3)
jquery-validation
(1)
jquery
(1)
bootstrap
(1)
dist
(0)
js
(12)
css
(32)
Views
(2)
Shared
(4)
obj
(5)
Debug
(0)
net8.0
(20)
staticwebassets
(4)
scopedcss
(0)
bin
(0)
vs
(0)
v17
(4)
FileContentIndex
(3)