2

I am completely new to ASP.NET Core(2 weeks in) and I need to learn Web dev all over again with ASP.net for work and yeah, trying to figure it all out. so forgive the probably noob question.

I am having issues passing data from HTML form to a Model and then to the Controller that I can't figure out. Here is my issue.

Here is basic HTML:

<form method="post" action="/FormProcWithModels" role="form">
    <input asp-for="UserName" type="text" id="UserName" placeholder="Enter your Full Name" />
    <input type="submit" />
</form>

Here is Model:

namespace project001.Models
{
    public class ContactPageModel
    {
        public string UserName{ get; set; }
    }
}

Here is the Controller:

Edit to show more code. This is my GET method

[HttpGet]
public IActionResult Contact()
{
   ViewBag.PageTitle = "This is the Contact page";
   return View();
}

[HttpPost("FormProcWithModels")]
public IActionResult Contact(ContactPageModel model)
{
    return Content($"The form username entered is {model.UserName}");
}

So as an example, when I enter the name of "Jim" into the form and submit it, the page loads with "The form username entered is:" but the name doesn't pass through.

I don't get errors or anything and I'm not good enough to figure out why the data is null.

Thanks in advance for any assistance given.

Edit:

When I do it like this:

[HttpPost("FormProcWithoutModels")]
public IActionResult Contact(string uName)
{
    string currentUser = uName;
    ViewBag.PageTitle = "This is the Contact page";
    //return View();
    return Content($"The form username entered is {currentUser}");
}

It works without Model. Soon as I try with Models, it doesn't work!

abatishchev
  • 98,240
  • 88
  • 296
  • 433
somdow
  • 6,268
  • 10
  • 40
  • 58
  • 1
    If this is just a basic learning project, consider uploading it to Github for someone to take a look at. There's not enough information in the question to resolve this. – Kirk Larkin Dec 16 '17 at 17:09
  • It could no possibly work with a parameter named `string uName` since the name of the input is `name="UserName"`, therefore you have not shown the relevant code. –  Dec 16 '17 at 22:25
  • Hi @StephenMuecke, thats another route. i put it to show how when i do data passing like that, that it works. notice the "FormProcwithoutModels" vs "FormProcWithModel" . sorry if i made it confusing – somdow Dec 16 '17 at 23:16
  • The code you have shown works just fine. If its not working for you, then its dues to other code you have not shown us. –  Dec 16 '17 at 23:55
  • 1
    I'm curious, when you say you're completely new to ASP.NET core and are having to learn web dev all over, what were you using before? I'm interested in the learning curve of ASP.NET Core and making it easier for people to ramp up on .NET Core... – Tony Ranieri Dec 17 '17 at 00:40
  • My expereince is in php/js and im so-so in the MEAN /nodes stack so most of these things in asp.net arent necessarily overwhelming (so far lol) but yeah.....The learning curve for me is kinda steep cause there is so much and i dont have someone to really guys me – somdow Dec 17 '17 at 01:58
  • Thanks @TonyRanieri, This project is basic so im not sure what other code can be interfiering. Im going to separate it into a new solution and see what happens – somdow Dec 17 '17 at 02:00

3 Answers3

2

I assume that this is because, you don't have get method. You need to add get method to take user input.

So, basically your controller looks like:

[HttpGet]
public IActionResult Contact()
{
    return View();
}

[HttpPost("FormProcWithModels")]
public IActionResult Contact(ContactPageModel model)
{
    return Content($"The form username entered is {model.UserName}");
}

View page:

@model ContactPageModel

<form method="post" action="/FormProcWithModels">
    <input asp-for="UserName" type="text" id="UserName" placeholder="Enter your Full Name" />
    <input type="submit" />
</form>

Though it's not working, add role="form"

<form method="post"...  role="form">

...
...
</form>
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Howdy Div. i do have a "get method" that looks exactly like yours in my code . – somdow Dec 16 '17 at 14:31
  • @somdow: Thanks for letting me know, I updated the answer, please check – Divyang Desai Dec 16 '17 at 14:33
  • @somdow: I have checked it on my end, and your code is really working.. – Divyang Desai Dec 16 '17 at 14:35
  • I updated my post to include my GET method plus the HTML where i added role="form". And nothing. I dont see any errors and when i put a breakpoint on the return content line, and mouse over "model", the value is null. Also, to be clear, this is on the top of my html form razor page: @model project001.Models.ContactPageModel and nothing. – somdow Dec 16 '17 at 14:39
  • @somdow: Not sure it's why it's not working, it's working for me..see here: https://imgr.es/3SI4 – Divyang Desai Dec 16 '17 at 14:48
  • WAO! lol. I see..........i dont know what to say, your code looks exactly like mine yet on my breakpoint, mine is "null". I added more code at the end of my post, That way it works but when i try this way. it doesnt. Can it be im maybe missing a directive(@Using) or....something else? Im lost and too much of a noob to know any other alternatives – somdow Dec 16 '17 at 14:51
  • @somdow: Nope, there is noting additional to add. I really not sure, what should be the problem then! I'm using `.NET Core SDK 2.0` with VS2017 which version are you using? alternative way is stated by @programtreasures but not the best practices for complex scenario!! – Divyang Desai Dec 16 '17 at 15:00
  • I downloaded VS 2 weeks ago so whichever one that is lol. i'll keep looking. Thanks for your assistance – somdow Dec 16 '17 at 15:47
2

You can also get submitted value using HttpContext.Request params

[HttpPost("FormProcWithModels")]
public IActionResult Contact()
{
    var UserName = HttpContext.Request.Form["UserName"]

    return Content($"The form username entered is {UserName}");
}

using FormCollection

[HttpPost("FormProcWithModels")]
public IActionResult Contact(FormCollection Fc)
{
    var UserName = Fc["UserName"].ToString();

    return Content($"The form username entered is {UserName}");
}
programtreasures
  • 4,250
  • 1
  • 10
  • 29
0

I’ve found that in ASP.Net Core, model binding isn’t always as automatic. Try:

[HttpPost("FormProcWithModels")]
public IActionResult Contact([FromForm] ContactPageModel model)
{
    return Content($"The form username entered is {model.UserName}");
}
Dan Soper
  • 639
  • 8
  • 18