简体   繁体   中英

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. 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. 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. 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. If you're new to .NET Web Development I suggest you get started with Entity Framework for your data access needs. more information:

You must store your data somewhere. For example you can store it in a session

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

I'm without Vs so can be errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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