0

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!

Just_Stacking
  • 395
  • 3
  • 13
  • Your `User` class's constructor has not initialised `Username`, so it is still null. – Matthew Watson Nov 06 '17 at 09:07
  • You havent made any empty lists which you should probably do in your constructor – BugFinder Nov 06 '17 at 09:07
  • I might be reading into something here but perhaps you wanted your model to be a `List`? – lc. Nov 06 '17 at 09:09
  • (at)MatthewWatson: Not sure how this is a duplicate. The question is how do I get my List<>. The NullReference is just a symptom. (at)BugFinder: This class works fine as long as no Lists are involved. (at)lc.: I tried your solution, but then it complains: "cannot convert from string to .Models.User" – Just_Stacking Nov 06 '17 at 09:28
  • Your problem is not getting the list - but that you never made the list to get so its null.. as explained before – BugFinder Nov 06 '17 at 09:33
  • @all: Thanks for pointing out my mistake, after many hours of going in circles, I guess I was just too tired to see the obvious. For any other beginners coming across this, after several hours of research I found this article which helped me to get the basic list started: http://www.c-sharpcorner.com/UploadFile/mahesh/create-a-list-of-objects-in-C-Sharp/ – Just_Stacking Nov 07 '17 at 01:23

0 Answers0