如何在 Asp.net MVC C# 中从会话而不是数据库插入和获取数据
How to insert and fetch Data from Sessions instead of Database in Asp.net MVC C#
现在我已经了解了在有或没有页面刷新的情况下应用 CRUD 操作。但现在我想要的是从会话而不是数据库中保存和检索数据。我从前两天一直在寻找这个问题,但我还没有找到任何可靠的资源可以解决或详细解释如何做到这一点?。
我知道在网上购物或在线答题系统的场景中,最好使用会话来存储临时数据,而不是将其保存到数据库中,因为资源消耗高。
我是这项技术的初学者,如果有人解决或分享一些可靠的教程链接以遵循它,那将是非常可观的,在此先感谢。
您可以像这样在您的控制器中声明一个全局会话变量和一个全局 GUID 数据类型变量(用于生成唯一 ID 作为主键)。
public List<Models.Details> SessionList
{
get
{
var obj = Session["myList"];
if (obj == null) { obj = Session["myList"] = new List<Models.Details>(); }
return (List<Models.Details>)obj;
}
set
{
Session["myList"] = value;
}
}
public Guid guid;
然后您可以在 post 方法中使用它们将数据插入到会话中,就像这样
public ActionResult Test(Models.Details[] itemlist)
{
foreach (Models.Details d in itemlist)
{
guid = Guid.NewGuid();
d.IdString = guid;
SessionList.Add(d);
}
return Json(new { success = true, id = itemlist[0].IdString, name = itemlist[0].Name, city = itemlist[0].City, country = itemlist[0].Country });
}
这里用returnjson返回对ajax调用的响应,要不要在[=42=末尾追加数据就看个人了] 你可以使用这行代码。如果您想在页面刷新时追加,只需添加 return Json("Ok")。
这是我的 Javascript 文件,它用于通过 ajax 调用读取和 post 数据到控制器方法,这有助于我们避免页面刷新。
$(document).ready(function(){
$("#buttonid").on("click", Submit);
function Submit() {
var itemlist = [];
//get cell values, instead of the header text.#tablePost > tbody > t
$("#tblData > tbody > tr").each(function () {
debugger;
var name = $(this).find("[name='Name[]']").val();
var city = $(this).find("[name='City[]']").val();
var country = $(this).find("[name='Country[]']").val();
var tdlist = $(this).find("td");
var Details = { Name: name, City: city, Country: country };
itemlist.push(Details);
})
console.log(itemlist)
$.ajax({
url: '/Student/Test',
dataType: "json",
data: JSON.stringify({ itemlist: itemlist }),
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.success) {
$("#tblDetails").append(
"<tr data-record-id=" + response.id + ">" +
"<td>" + response.name + "</td>" +
"<td>" + response.city + "</td>" +
"<td>" + response.country + "</td>" +
"<td> <a class='edit-record' data-model-id=" + response.id + " onclick='EditGet(this)'><img src='/UserImages/edit.png'/></a><a class='delete' data-model-id=" + response.id + " onclick='DeleteGet(this)'><img src='/UserImages/delete.png'/></a></td>" +
"</tr>"
);
}
},
error: function () {
alert("error");
}
});
};
});
这将有望在会话中插入您的数据。
要以 tabled 列表的形式查看插入的数据,您可以使用如下所示的 get 方法:
public ActionResult Test()
{
List<Models.Details> detaillist = new List<Models.Details>();
var data = SessionList;
var query = data.Select(x => new Models.Details
{
IdString = x.IdString,
Name = x.Name,
City = x.City,
Country = x.Country
});
detaillist = query.ToList();
return View(detaillist);
}
这是用于将数据插入视图的剃须刀视图 Table :
@model List<Inventory.Models.Details>
<button id="btnSubmit">Submit Data</button>
@using (Html.BeginForm("Test", "Student", FormMethod.Post))
{
<table id="tblData" class=" table-responsive table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody></tbody>
</table>
<input type="button" value="submit" id="btnInsert" />
<table id="tblDetails" class=" table-responsive table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr data-record-id="@item.Id">
<td>@item.Name</td>
<td>@item.City</td>
<td>@item.Country</td>
<td>
<a class="edit-record" data-model-id="@item.Id" onclick="EditGet(this)" href="javascript:;">
<img id="image" src="/UserImages/edit.png" />
</a>
<a class="deleteimage" onclick="DeleteGet(this)" data-model-id="@item.Id">
<img src="/UserImages/delete.png" />
</a>
</td>
</tr>
}
</tbody>
</table>}
正如您上面提到的,您已经完成了所有 CRUD 操作,这个答案只是关于如何从会话中插入和获取数据的想法。通过遵循与此相同的策略,将帮助您完成 CRUD 中的所有剩余操作。希望这篇 post 对您有所帮助。
现在我已经了解了在有或没有页面刷新的情况下应用 CRUD 操作。但现在我想要的是从会话而不是数据库中保存和检索数据。我从前两天一直在寻找这个问题,但我还没有找到任何可靠的资源可以解决或详细解释如何做到这一点?。
我知道在网上购物或在线答题系统的场景中,最好使用会话来存储临时数据,而不是将其保存到数据库中,因为资源消耗高。
我是这项技术的初学者,如果有人解决或分享一些可靠的教程链接以遵循它,那将是非常可观的,在此先感谢。
您可以像这样在您的控制器中声明一个全局会话变量和一个全局 GUID 数据类型变量(用于生成唯一 ID 作为主键)。
public List<Models.Details> SessionList
{
get
{
var obj = Session["myList"];
if (obj == null) { obj = Session["myList"] = new List<Models.Details>(); }
return (List<Models.Details>)obj;
}
set
{
Session["myList"] = value;
}
}
public Guid guid;
然后您可以在 post 方法中使用它们将数据插入到会话中,就像这样
public ActionResult Test(Models.Details[] itemlist)
{
foreach (Models.Details d in itemlist)
{
guid = Guid.NewGuid();
d.IdString = guid;
SessionList.Add(d);
}
return Json(new { success = true, id = itemlist[0].IdString, name = itemlist[0].Name, city = itemlist[0].City, country = itemlist[0].Country });
}
这里用returnjson返回对ajax调用的响应,要不要在[=42=末尾追加数据就看个人了] 你可以使用这行代码。如果您想在页面刷新时追加,只需添加 return Json("Ok")。
这是我的 Javascript 文件,它用于通过 ajax 调用读取和 post 数据到控制器方法,这有助于我们避免页面刷新。
$(document).ready(function(){
$("#buttonid").on("click", Submit);
function Submit() {
var itemlist = [];
//get cell values, instead of the header text.#tablePost > tbody > t
$("#tblData > tbody > tr").each(function () {
debugger;
var name = $(this).find("[name='Name[]']").val();
var city = $(this).find("[name='City[]']").val();
var country = $(this).find("[name='Country[]']").val();
var tdlist = $(this).find("td");
var Details = { Name: name, City: city, Country: country };
itemlist.push(Details);
})
console.log(itemlist)
$.ajax({
url: '/Student/Test',
dataType: "json",
data: JSON.stringify({ itemlist: itemlist }),
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (response) {
if (response.success) {
$("#tblDetails").append(
"<tr data-record-id=" + response.id + ">" +
"<td>" + response.name + "</td>" +
"<td>" + response.city + "</td>" +
"<td>" + response.country + "</td>" +
"<td> <a class='edit-record' data-model-id=" + response.id + " onclick='EditGet(this)'><img src='/UserImages/edit.png'/></a><a class='delete' data-model-id=" + response.id + " onclick='DeleteGet(this)'><img src='/UserImages/delete.png'/></a></td>" +
"</tr>"
);
}
},
error: function () {
alert("error");
}
});
};
});
这将有望在会话中插入您的数据。
要以 tabled 列表的形式查看插入的数据,您可以使用如下所示的 get 方法:
public ActionResult Test()
{
List<Models.Details> detaillist = new List<Models.Details>();
var data = SessionList;
var query = data.Select(x => new Models.Details
{
IdString = x.IdString,
Name = x.Name,
City = x.City,
Country = x.Country
});
detaillist = query.ToList();
return View(detaillist);
}
这是用于将数据插入视图的剃须刀视图 Table :
@model List<Inventory.Models.Details>
<button id="btnSubmit">Submit Data</button>
@using (Html.BeginForm("Test", "Student", FormMethod.Post))
{
<table id="tblData" class=" table-responsive table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody></tbody>
</table>
<input type="button" value="submit" id="btnInsert" />
<table id="tblDetails" class=" table-responsive table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr data-record-id="@item.Id">
<td>@item.Name</td>
<td>@item.City</td>
<td>@item.Country</td>
<td>
<a class="edit-record" data-model-id="@item.Id" onclick="EditGet(this)" href="javascript:;">
<img id="image" src="/UserImages/edit.png" />
</a>
<a class="deleteimage" onclick="DeleteGet(this)" data-model-id="@item.Id">
<img src="/UserImages/delete.png" />
</a>
</td>
</tr>
}
</tbody>
</table>}
正如您上面提到的,您已经完成了所有 CRUD 操作,这个答案只是关于如何从会话中插入和获取数据的想法。通过遵循与此相同的策略,将帮助您完成 CRUD 中的所有剩余操作。希望这篇 post 对您有所帮助。