4

I have a problem with getting/handling POST data from <form> in ASP.NET MVC 4 Razor project.

What have I done?

Don't argue at my code, I'm only testing ASP.NET MVC 4 features.

As you can see in the controller's code, I've done also a Model for user info:

public class AuthForm
{
    public string Login { get; set; }
    public string Password { get; set; }
}

I thought, that ASP.NET if the model is correct will automatically parse data into the model, but mistaken.

Then I tried to use .Request["name"], but (inputs weren't empty):

enter image description here

Also I've tried to use such Attributes:

  • [HttpPost]
  • [AcceptVerbs(HttpVerbs.Post)]

But also no success!

What did I wrong? Please, help me to fix my issues.

Thanks

1 Answers1

2

You need to use the helper methods so that MVC knows how to bind the values and then in the controller you will be able to use the model (as the model binder will figure it out for you)

e.g.

@model Models.AuthForm
@{
    ViewBag.Title = "СЭЛФ";
}
@section home {

@using (Html.BeginForm("Auth", "Controller")) {
    <div class="control-group">
        @Html.LabelFor(model => model.Login, new { @class = "control-label" })
        <div class="controls">
            @Html.TextBoxFor(model => model.Login, new { @class = "input-large", autocapitalize = "off" }) 
            @Html.ValidationMessageFor(model => model.Login, "*", new { @class = "help-inline" })
       </div>
    </div>

    <div class="control-group">
        @Html.LabelFor(model => model.Password, new { @class = "control-label" })
        <div class="controls">
            @Html.PasswordFor(model => model.Password, new { @class= "input-large" }) 
            @Html.ValidationMessageFor(model => model.Password, "*", new { @class = "help-inline" })
        </div>
    </div>
    <div class="form-actions">
         <input type="submit" class="btn btn-primary" value="Log On" />
    </div>
}
}

Using the native HTML controls is an option but you'll need to do it in the same way as the helper methods above otherwise the model won't be populated.

Richard Harrison
  • 19,247
  • 4
  • 40
  • 67
  • Thanks, for the help :) but , there is another problem... View doesn't see the @model Models.AuthForm and gives an error: *The type or namespace name 'Models' could not be found (are you missing a using directive or an assembly reference?)* –  Jun 24 '13 at 05:58
  • Assuming that the namespace is correct (i.e. that it is Models.Authform that we're working with) change it to AuthForm and then hover over it and Visual Studio should tell you what the namespace is. – Richard Harrison Jun 24 '13 at 06:00
  • http://ideone.com/6JteJP - models has the next namespace in the code: *namespace MvcApplication6.Models* and I've tried to use also features in http://stackoverflow.com/questions/4953330/razor-based-view-doesnt-see-referenced-assemblies but neither editing **web.config** in View's folder, neither just including like **@using MvcApplication6.Models;** don't help me :( –  Jun 24 '13 at 06:09
  • thanks for your answer again, you know all begin to work with a such code: http://ideone.com/pNHb7b –  Jun 24 '13 at 06:27
  • it's easiest if you keep the models in the same project, and change the definition to namespace Models rather than MvcApplication6.Models; don't have to many namespaces and choose their names wisely. – Richard Harrison Jun 24 '13 at 06:51
  • I know, that it's the easiest, but you may be don't understand me well :) My project is so small (because I'm only testing mvc razor, because it's new for me), that there is only the one namespace for the project. And I really don't understand why did both web.config and @using don't work, because the models are in the same project. –  Jun 24 '13 at 06:58
  • it has to be something else because I don't normally have to add model namespaces to web.config. try @model MvcApplication6.Models.AuthForm – Richard Harrison Jun 24 '13 at 07:38