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!

7
  • 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. Commented Dec 16, 2017 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. Commented Dec 16, 2017 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 Commented Dec 16, 2017 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. Commented Dec 16, 2017 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... Commented Dec 17, 2017 at 0:40

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

8 Comments

Howdy Div. i do have a "get method" that looks exactly like yours in my code .
@somdow: Thanks for letting me know, I updated the answer, please check
@somdow: I have checked it on my end, and your code is really working..
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: Not sure it's why it's not working, it's working for me..see here: imgr.es/3SI4
|
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}");
}

2 Comments

Hello @programtreasures. I did the first one and no good. For the second one, what "using" do i need to add for it to work?
using Microsoft.AspNetCore.Http
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}");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.