如何将 C#.NET MVC 中的用户输入数据保存到列表中?
How do I save user input data in C#.NET MVC to a list?
我对使用 MVC 模式非常陌生,但我想做的是让用户输入一些数字,然后每次他们提交一个数字时,它就会被添加到一个列表中。然而,列表在按下按钮之间被擦除,我目前在控制器中拥有所有列表内容。
控制器
public class NumberController : Controller
{
List<int> numList = new List<int>();
// GET: Number
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ProcessNumber(Number number)
{
int RNo = number.RNum;
numList.Add(RNo);
string sequenceList = "";
foreach (int num in numList) { sequenceList = sequenceList + ", " + num; }
ViewBag.Message = "Number " + RNo + " saved successfully!" + " This is your current sequence :" + sequenceList + " A total of " + numList.Count + "numbers.";
return View("Index");
}
}
然后在视图中我有这个。
@model TechMVC.Models.Number
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm("ProcessNumber", "Number", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Number</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RNum, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
<p>@ViewBag.Message</p>
</div>
</div>
</div>
}
结果是这样的:
我对让它看起来很花哨不感兴趣,我只是对 MVC 框架还不太确定。我知道我已经将数据从用户发送到视图,然后再发送到控制器,但我是否也在某些时候使用了模型?我应该将列表存储在模型中还是视图中?
您不能在控制器变量中保留这些值。 Web 是无状态的,每个 HTTP 请求都会执行您的代码的一个新实例。因此,您需要将数据保存在某处。
您可以为此使用会话状态 或应用程序状态。更多阅读:
- ASP.NET Session State Overview
- Session and Application states in ASP.NET Core
- Using ASP.NET MVC TempData and Session to pass values across Requests
- Managing Controller Specific Session in ASP.NET MVC
在实际应用程序中,您肯定需要数据库存储。无论哪个商店,您都可以使用 Sqlite、SQL Server、MySQL、XML 文档或任何您想要的东西。如果您是 .NET Web 开发的新手,我建议您开始使用 Entity Framework 来满足您的数据访问需求。更多信息:
您必须将数据存储在某个地方。例如,您可以将其存储在会话中
List numList = null;
int rNo = number.RNum;
string sequenceList = string.Empty;
if (Session["NumList"] != null)
{
//session exist. set numList = session
numList = Session["NumList"] as List<int>;
}
else {
//session not exist. set numList as new
numList = new List<int>();
}
//add number to numList
numList.Add(rNo);
//set session = numList
Session["NumList"] = numList;
//make join oj numList
sequenceList = String.Join(", ", numList.ToArray());
//set message in a ViewBag
ViewBag.Message = $"Number {rNo} saved successfully!" +
$" This is your current sequence : {sequenceList}" +
$" A total of {numList.Count} numbers.";
return View("Index");
我没有 Vs 所以可能会出错
我对使用 MVC 模式非常陌生,但我想做的是让用户输入一些数字,然后每次他们提交一个数字时,它就会被添加到一个列表中。然而,列表在按下按钮之间被擦除,我目前在控制器中拥有所有列表内容。
控制器
public class NumberController : Controller
{
List<int> numList = new List<int>();
// GET: Number
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ProcessNumber(Number number)
{
int RNo = number.RNum;
numList.Add(RNo);
string sequenceList = "";
foreach (int num in numList) { sequenceList = sequenceList + ", " + num; }
ViewBag.Message = "Number " + RNo + " saved successfully!" + " This is your current sequence :" + sequenceList + " A total of " + numList.Count + "numbers.";
return View("Index");
}
}
然后在视图中我有这个。
@model TechMVC.Models.Number
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm("ProcessNumber", "Number", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Number</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RNum, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RNum, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RNum, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
<p>@ViewBag.Message</p>
</div>
</div>
</div>
}
结果是这样的:
我对让它看起来很花哨不感兴趣,我只是对 MVC 框架还不太确定。我知道我已经将数据从用户发送到视图,然后再发送到控制器,但我是否也在某些时候使用了模型?我应该将列表存储在模型中还是视图中?
您不能在控制器变量中保留这些值。 Web 是无状态的,每个 HTTP 请求都会执行您的代码的一个新实例。因此,您需要将数据保存在某处。
您可以为此使用会话状态 或应用程序状态。更多阅读:
- ASP.NET Session State Overview
- Session and Application states in ASP.NET Core
- Using ASP.NET MVC TempData and Session to pass values across Requests
- Managing Controller Specific Session in ASP.NET MVC
在实际应用程序中,您肯定需要数据库存储。无论哪个商店,您都可以使用 Sqlite、SQL Server、MySQL、XML 文档或任何您想要的东西。如果您是 .NET Web 开发的新手,我建议您开始使用 Entity Framework 来满足您的数据访问需求。更多信息:
您必须将数据存储在某个地方。例如,您可以将其存储在会话中
List numList = null; int rNo = number.RNum; string sequenceList = string.Empty;
if (Session["NumList"] != null) { //session exist. set numList = session numList = Session["NumList"] as List<int>; } else { //session not exist. set numList as new numList = new List<int>(); } //add number to numList numList.Add(rNo); //set session = numList Session["NumList"] = numList; //make join oj numList sequenceList = String.Join(", ", numList.ToArray()); //set message in a ViewBag ViewBag.Message = $"Number {rNo} saved successfully!" + $" This is your current sequence : {sequenceList}" + $" A total of {numList.Count} numbers."; return View("Index");
我没有 Vs 所以可能会出错