简体   繁体   English

如何将C#.NET MVC中的用户输入数据保存到列表中?

[英]How do I save user input data in C#.NET MVC to a list?

I'm very much new to using the MVC Pattern but what I'm trying to do is have a user input some numbers and then each time they submit a number it'll be added to a list. 我对使用MVC模式非常陌生,但是我想做的是让用户输入一些数字,然后每次他们提交数字时,它都会被添加到列表中。 However the list gets wiped between button presses, I currently have all the list stuff in the controller. 但是,列表在两次按下按钮之间就被擦除了,我目前在控制器中拥有所有列表内容。

Controller 控制者

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");
    }
}

Then over in the View I have this. 然后在视图中,我有这个。

@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>
}

Which comes out looking like this : 看起来像这样:

网站版本

I'm not interested in making it look fancy, I'm just still not overly certain of the MVC framework. 我对使它看起来花哨不感兴趣,只是我仍然不太确定MVC框架。 I get that I've sent Data here from a user to the view and then to the controller but have I also used the model at some point? 我知道我已经在这里将数据从用户发送到视图,然后再发送到控制器,但是在某个时候我是否也使用过模型? Should I store the list in the model or in the view? 我应该将列表存储在模型中还是在视图中?

You cannot retain these values in a controller variable. 您不能将这些值保留在控制器变量中。 The Web is stateless, and each HTTP request executes a new instance of your code. Web是无状态的,并且每个HTTP请求都将执行代码的新实例。 So you'll need to persist your data somewhere. 因此,您需要将数据保留在某处。

You could utilize the Session State or Application State for that matter. 您可以利用会话状态应用程序状态来解决此问题。 For more reading: 欲了解更多信息:

In a real-world application you'll definitely need a database storage. 在实际的应用程序中,您肯定需要数据库存储。 doesn't matter which store, you could use Sqlite, SQL Server, MySQL, XML documents or anything you want. 不管哪个存储都可以,您可以使用Sqlite,SQL Server,MySQL,XML文档或任何您想要的东西。 If you're new to .NET Web Development I suggest you get started with Entity Framework for your data access needs. 如果您不熟悉.NET Web开发,我建议您开始使用Entity Framework来满足您的数据访问需求。 more information: 更多信息:

You must store your data somewhere. 您必须将数据存储在某个地方。 For example you can store it in a session 例如,您可以将其存储在会话中

List numList = null; List numList = null; int rNo = number.RNum; int rNo = number.RNum; string sequenceList = string.Empty; 字符串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"); 

I'm without Vs so can be errors 我没有Vs,所以可能会出错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM