10

I'm hitting what I think is a pretty stupid issue that I am obviously missing something simple on.

I made a simple asp.net mvc site (.net 4.5) and changed the index to have a simple form that I'd like to just post back to itself and spit back the variables.
here is my form

@using(Html.BeginForm())
{
    <input type="text" class="form-control" id="empId" placeholder="Enter EmployeeId (ex. 999999)">
    <input type="text" class="form-control" id="account" placeholder="Enter account)">
    <input type="email" class="form-control" id="email" placeholder="Enter email">
    <input type="submit" class="btn btn-default" value="Submit" />
}

and here is my post method

[HttpPost]
public ActionResult Index(string empId, string account, string email)
{
    return Content(Request["empId"]);
}

I get nothing back when the page posts. Also in the debugger I can see that the method gets hit, however all the parameters are null even though I filled in the form.

Am I missing something obvious?

4
  • 1
    add name in addition to id: name="empId", name="account", name="email", ... And after your tests, please consider use model binding and model, it is more clean/smart :-) Commented May 19, 2015 at 14:24
  • thanks! that solved my problem. i'm just going to give up on programming for the day Commented May 19, 2015 at 14:26
  • cool, I made a full answer, please consider to validate it if it's ok Commented May 19, 2015 at 14:33
  • In my case, my model properties were Fields and not C# properties. Commented Nov 14, 2016 at 5:16

1 Answer 1

16

You just forget the name attribute:

@using(Html.BeginForm())
{
    <input type="text" class="form-control" name="empId" id="empId" placeholder="Enter EmployeeId (ex. 999999)">
    <input type="text" class="form-control" name="account" id="account" placeholder="Enter account)">
    <input type="email" class="form-control" name="email" id="email" placeholder="Enter email">
    <input type="submit" class="btn btn-default" value="Submit" />
}

I always recommend to use model binding instead of some strings or int. If you use them well, it will make the model binding work effortlessly:

Model:

public class ExampleModel
{
public int empId { get; set; }
public string account{ get; set; }
public string email{ get; set; }
}

In the Razor page:

@using(Html.BeginForm())
    {
        @Html.EditorFor((m => m.intempId, new { @class = "form-control" } ))
        @Html.EditorFor((m => m.account, new { @class = "form-control" }))
        @Html.EditorFor((m => m.email, new { @class = "form-control" }))
    }

and then in controller:

[HttpPost]
public ActionResult Index(ExampleModel model)
{
    return Content(model.empId);
}

With the model, you can also add validation and so on, directly on the model and then ASP.NET MVC can put validation in both front-end with jQuery validation and back-end (if (ModelState.IsValid)). Lots of benefits to use models!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for providing the OP with the correct way to handle these things in MVC :)
A little bit of help is always welcome when you start new technology :-)

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.