[英]The best way to save the String Variable after being redirected Page in ASP.NET CORE
How can I save the form values as a string after the page is redirected?页面重定向后如何将表单值保存为字符串?
I use a static variable, if the number of users on the site increases for example 20000, will the site have problems?我使用一个 static 变量,如果站点上的用户数增加例如 20000,站点会有问题吗?
Which method is better?哪种方法更好?
Using a static variable?使用 static 变量?
session? session?
Cookies? Cookies?
ViewBag?查看包?
View State?查看 State?
sql server database? sql 服务器数据库?
Actually, There is no the best method, Only the most suitable method.其实,没有最好的方法,只有最合适的方法。 The methods you list above all have their own most suitable use cases.您上面列出的方法都有自己最合适的用例。
From your comment, I think you may wanna redirect to another page with a string parameter, So you can try to use:根据您的评论,我认为您可能想使用字符串参数重定向到另一个页面,因此您可以尝试使用:
return RedirectToAction("actionName","controllerName",new { Name="xxx"});
It will send a get request: acontrollerName/actionName?Name=xxx
.它将发送一个获取请求: acontrollerName/actionName?Name=xxx
。
Or, if you wanna save that string value for a long time, You can also try to use Session
or Database
.或者,如果您想长时间保存该字符串值,您也可以尝试使用Session
或Database
。 But one thing you need to consider is that you need to set the session time out according to the actual situation of your project.但是您需要考虑的一件事是您需要根据项目的实际情况设置session超时。
==========================edited=========================== ===========================编辑======================= ====
I think you can use Session to achieve it.我认为您可以使用 Session 来实现它。
public class TestModel
{
public int ID { get; set; }
public string Name { get; set; }
}
public async Task<IActionResult> OnPostSelectMenu(int selectedid)
{
TestModel selected = await _db.Table.FindAsync(selectedid);
//set session
HttpContext.Session.SetString("Key", JsonConvert.SerializeObject(selected));
return RedirectToPage("MyPage");
}
public async Task<IActionResult> OnGetAsync()
{
//......
//get session
Input = JsonConvert.DeserializeObject<TestModel>(HttpContext.Session.GetString("Key"));
return Page();
}
no i dont want to redirect to other page i want give string value after refresh current page How can I save the form values as a string after the page is refreshed?不,我不想重定向到其他页面我想在刷新当前页面后给出字符串值刷新页面后如何将表单值保存为字符串?
this is my model:这是我的 model:
public int ID { get; set; }
public string Name { get; set; }
static variable: static 变量:
public static int IDWordsaver = 0;
On Get for MyPage:在获取 MyPage 时:
public async Task<IActionResult> OnGetAsync()
{
await LoadAsync();
return Page();
}
private async Task LoadAsync()
{
if (IDWordsaver != 0)
{
var SetRecord = _db.Table.FirstOrDefault(u => u.ID == IDWordsaver);
ID = IDWordsaver;
Input = new InputModel
{
Name = SetRecord.Name
};
}
}
i use this button for select one record from data base:我将此按钮用于 select 数据库中的一条记录:
<button asp-page-handler="SelectMenu" name="selectedid" value=@item.ID class="btn btn-primary" formnovalidate>Select</button>
public async Task<IActionResult> OnPostSelectMenu(int selectedid)
{
var selected = await _db.Table.FindAsync(selectedid);
ID = selected.ID;
Input.Name = selected.Name;
IDWordsaver = selectedid;
return RedirectToPage("MyPage", new { IDWordsaver });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.