ProgrammingAsk
ProgrammingAsk

Reputation: 65

ASP.NET MVC Core - Post form to class parameter

            <form method="post" id="modalForm" action="/Test/TestForm" target="myframe">
                <input type="text" name="id" required />
                <input type="text" name="name" required />
                <input type="submit" value="Post" onsubmit="alert('after post!');" />
            </form>

    [HttpPost]
    public IActionResult TestForm(Test test)
    {
        return View();
    }

    public class Test
    {
        public string id;
        public string name;
    }

What needs to be changed, on the form, to get values to the Test class? Currently the values are null? I would like to keep it simple, without ASP.NET Core helpers.

Upvotes: 1

Views: 712

Answers (1)

Abdul Hadi
Abdul Hadi

Reputation: 1228

C# provides a way to use short-hand / automatic properties where you only have to write get; and set; inside the property.

    public class Test
    {
        public string id { get; set; }
        public string name { get; set; }
    }

Upvotes: 1

Related Questions