Shuyuntake
Shuyuntake

Reputation: 79

Post method always return null MVC4

My controller :

        [HttpPost]
        public void setInfoUser(string str)
        {
            Demandeur demandeur = new Demandeur(str);
        }

My view :

 @using (Html.BeginForm("setInfoUser", "Personne", FormMethod.Post))
    {
       <input type="text" id="idUser" value="test" />
       <input type="submit" value="ok" name="setInfoUser" />
   }

The method is fired but the string is always null. What am I doing wrong?

Upvotes: 0

Views: 73

Answers (2)

Nikitesh
Nikitesh

Reputation: 1305

You should use model based approach since you are using mvc
View:

@model Your_NameSpace.Demandeur 
@using (Html.BeginForm("setInfoUser", "Personne", FormMethod.Post))
 {
  @Html.EditorFor(model=>model.UserName)
  <input type="submit" value="ok" name="setInfoUser" />
 }

Controller

[HttpPost]
public void setInfoUser(Demandeur demandeur)
 {
   //your logic ahead
 }

Upvotes: 0

vortex
vortex

Reputation: 1058

Set name attribute to be equal to your string variable.

 <input type="text" id="idUser" name="str" value="test" />

Upvotes: 2

Related Questions