1

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 :

Website Version

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?

13
  • Because the web is stateless. Each request to your server initializes a new controller. You need to persist your data somewhere (ideally a database) Commented Dec 15, 2017 at 10:36
  • I think the input I'm currently gathering would be overkill for a database, as I want the user to be able to sort their numbers after they've input them. Would that not require a wipe of the DB for each different user? Commented Dec 15, 2017 at 10:40
  • Is it feasible to stored your List as a Session Variable? Commented Dec 15, 2017 at 10:42
  • I don't think the session is a great idea. How are you planning to keep the values for the subsequent request? Commented Dec 15, 2017 at 10:44
  • My original idea was to collect all the user input within the view before sending a request to the controller to handle all the inputs at once, so sort them into a sequence and store this sequence in a DB with the way it was sorted and a sorting time Commented Dec 15, 2017 at 10:47

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.