C# 如何在Asp.net MVC中从会话而不是数据库插入和获取数据#
现在我已经了解了应用CRUD操作时是否有页面刷新。但现在我想要的是从会话而不是数据库中保存和检索数据。从前两天开始,我一直在寻找这件事,但我没有找到任何可信的资源来解决或详细解释如何解决这件事 我知道,在在线购物或在线测验系统的场景中,最好使用会话来存储临时数据,而不是将其保存到数据库中,这会因为资源的高消耗而变得昂贵C# 如何在Asp.net MVC中从会话而不是数据库插入和获取数据#,c#,session,asp.net-mvc-5,C#,Session,Asp.net Mvc 5,现在我已经了解了应用CRUD操作时是否有页面刷新。但现在我想要的是从会话而不是数据库中保存和检索数据。从前两天开始,我一直在寻找这件事,但我没有找到任何可信的资源来解决或详细解释如何解决这件事 我知道,在在线购物或在线测验系统的场景中,最好使用会话来存储临时数据,而不是将其保存到数据库中,这会因为资源的高消耗而变得昂贵 我是这项技术的初学者,如果有人解决或分享一些可靠的教程链接,遵循它将是非常可观的,提前感谢 您可以像这样在控制器中声明全局会话变量和全局GUID数据类型变量(用于生成作为主键的唯
我是这项技术的初学者,如果有人解决或分享一些可靠的教程链接,遵循它将是非常可观的,提前感谢 您可以像这样在控制器中声明全局会话变量和全局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;
return json在这里用于返回对ajax调用的响应,这取决于您是否希望在表的末尾追加数据,您可以使用这行代码。如果您想在页面上追加刷新,只需添加返回Json(“Ok”)
这是我的Javascript文件,用于通过ajax调用读取数据并将数据发布到控制器方法,这有助于我们避免页面刷新
$(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");
}
});
};
});
$(文档).ready(函数(){
$(#按钮)点击提交;
函数提交(){
var itemlist=[];
//获取单元格值,而不是标题文本。#tablePost>tbody>t
$(“#tblData>tbody>tr”)。每个(函数(){
调试器;
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};
项目列表。推送(详细信息);
})
console.log(itemlist)
$.ajax({
url:“/Student/Test”,
数据类型:“json”,
数据:JSON.stringify({itemlist:itemlist}),
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(响应){
if(response.success){
$(“#tblDetails”)。追加(
"" +
“”+response.name+“”+
“”+response.city+“”+
“”+response.country+“”+
"
}
}
正如您在上面提到的,您已经完成了所有CRUD操作,这个答案只是一个如何从会话中插入和获取数据的概念。通过遵循与此相同的策略,将帮助您完成CRUD中的所有剩余操作。我希望这篇文章能帮到您。如果您需要进一步澄清,请d查询以解决此问题请在此处ping我。您尝试的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");
}
});
};
});
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);
}
@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>}