返回筆記列表
/ 筆記詳情
設定字典變數並顯示單筆資料¶
// Index動作
public IActionResult Index()
{
Dictionary<string, dynamic> message = new Dictionary<string, dynamic> {
{"id",1 },{"pdate",DateTime.Now},{"content","資管三乙"}
};
ViewBag.message = message;
return View();
}
@{
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>
設定包含三筆資料的字典清單並顯示¶
// Index動作
public IActionResult Index()
{
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();
}
@{
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>
定義資料類別 Message¶
放在 Models 資料夾中的 Message.cs:
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;
}
}
使用 Message 類別於 Index 動作與檢視¶
// Index動作
public IActionResult Index(int id, int btn)
{
List<Message> _messages = new List<Message>
{
new Message(1, DateTime.Now, "資管三甲"),
new Message(2, DateTime.Now, "資管三乙"),
new Message(3, DateTime.Now, "資管三丙")
};
ViewBag.messages = null; // 可依需求保留或移除
return View(_messages);
}
@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>
附件 137
WebApplication1
(10)
Views
(4)
Shared
(8)
Bank
(4)
Controllers
(2)
bin
(0)
Debug
(0)
net8.0
(6)
wwwroot
(6)
lib
(0)
jquery-validation
(1)
jquery-validation-unobtrusive
(3)
jquery
(1)
bootstrap
(1)
dist
(0)
js
(12)
css
(32)
obj
(5)
Debug
(0)
net8.0
(18)
scopedcss
(0)
staticwebassets
(4)