I am currently learning .net Core 2 and I would like to understand the basic ways to communicate between the Model, the View and the Controller.
So here my basic example, which works fine until I try to evolve it to something more complex using a List<>.
User Class:
using System;
using System.Collections.Generic;
namespace MySystem.Models
{
public class User
{
public User()
{
}
public string Username { get; set; }
public string Age { get; set; }
}
}
Related controllers:
public ActionResult AddWithHTMLHelperAndModel()
{
var usermodel = new User();
return View(usermodel);
}
[HttpPost]
public ActionResult AddWithHTMLHelperAndModel(User user)
{
var updated_model = user;
return View(updated_model);
}
My View:
@model MySystem.Models.User
@using (Html.BeginForm("AddWithHTMLHelperAndModel", "User", FormMethod.Post))
{
<label>Model's username: @Model.Username</label><br>
if (Model.Username == "")
{
@:UserName : @Html.TextBoxFor(m => m.Username)
@:Age : @Html.TextBoxFor(m => m.Age)
<input type="submit" asp-controller="Home" value="Submit" />
}
<label>in else statement</label><br>
@:UserName : <br>@Html.TextBoxFor(m => m.Username, new { placeholder = Html.DisplayNameFor(m => m.Username}))
<br>
@:Age :
<br> @Html.TextBoxFor(m => m.Age, new { placeholder = Html.DisplayNameFor(m => m.Username}))
<input type="submit" asp-controller="Home" value="Submit" />
}
Above stops working if I try to change my Model to:
using System;
using System.Collections.Generic;
namespace MySystem.Models
{
public class User
{
public User()
{
}
public List<String> Username { get; set; }
public List<String> Age { get; set; }
}
}
My expectation would have been that I can simply extend my Model to a List of Usernames and Ages, manipulate this List in the view and then for example save a consolidated list of items to a DB (have not started with the DBs yet.)
Basically what I was trying to do was then something like this in the related view:
@model MySystem.Models.User
@using (Html.BeginForm("AddWithHTMLHelperAndModel", "User", FormMethod.Post))
{
<label>Model's username: @Model.Username</label><br>
if (Model.Username == "")//This is expected be executed on first run.
{
@:UserName1 : @Html.TextBoxFor(m => m.Username[0])
@:Age1 : @Html.TextBoxFor(m => m.Age[0])
@:UserName2 : @Html.TextBoxFor(m => m.Username[1])
@:Age2 : @Html.TextBoxFor(m => m.Age[1])
<input type="submit" asp-controller="Home" value="Submit" />
}
<label>in else statement</label><br> //In case submitted more than once.
@:UserName : <br>@Html.TextBoxFor(m => m.Username[0], new { placeholder = Html.DisplayNameFor(m => m.Username[0])})
<br>
@:Age :
<br> @Html.TextBoxFor(m => m.Age[0], new { placeholder = Html.DisplayNameFor(m => m.Username[0])})
<input type="submit" asp-controller="Home" value="Submit" />
}
I have even tried to run this only in my command line by creating a new object of the User class:
var str = new User();
str.Username.Insert(0, "something");
Above 2 lines will always fail: Exception thrown: 'System.NullReferenceException'
Based on this short tutorial: http://www.dotnetodyssey.com/2015/03/24/getting-textbox-input-value-in-asp-net-mvc/
Please do not look for any deeper meaning in this code, as it is only aimed at helping me understand MVC.
I have tried to keep this very simple, but later on this would probably evolve into something that contains lists of lists (in principle multiple lists of Username-password for example).
Many thanks in advance!