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?
