我目前有一个可以正常工作的MVC应用程序,可以直接将数据加载到html表中,但是我的目标是如果按下按钮后将按钮简单地将数据放到表上就可以加载它。
这是我当前的视图 我的设计所在或索引已加载到哪里:
@{
ViewBag.Title = "Index";
}
@model IEnumerable<Entry.Models.util>
@section Styles {
<link href="@Url.Content("~/Content/main.css")" rel="stylesheet" type="text/css" />
}
<html>
<body>
<div id="table-wrapper">
<div id="table-scroll">
<table border="1">
<tr>
<th>HeaderA</th>
<th>HeaderB</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td width="50%">@item.name</td>
<td>
@Html.ActionLink(
"Add",
"Add",
new { id = item.serial},
new
{
@class = "glyphicon glyphicon-plus-sign",
onclick = "return confirm('Are you sure you want to add " + @item.name + " to list ?');"
})
</td>
</tr>
}
</table>
</div>
</div>
这是我的控制器 我对mysql的查询已加载到哪里:
public ActionResult Index()
{
//Load list table from MySql
List<util> myList = new List<util>();
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "SELECT serial, name FROM tableA";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
try
{
myList.Add(new util
{
serial= Convert.ToInt32(sdr["serial"]),
name = sdr["name"].ToString()
});
}
catch (Exception e)
{
Console.WriteLine("An error occurred: '{0}'", e);
}
}
}
con.Close();
}
}
return View(myList);
}
我尝试引用此链接,但由于我是该平台的新手,https://forums.asp.net/t/2006507.aspx?How+to+Load+Contents+from+table+only+after+click+a+button+
我还是很精通ORM方法任何建议/评论。 TIA