0

I don't know why its not working there is nothing complicated. I am just passing model to view and then getting the model back from view for updating.

Controller :

    [HttpGet]
    [ActionName("Test")]
    public ActionResult Test_get()
    {
        Testing t = new Testing();
        t.a = 100;
        t.name = "ali";
        t.age = "29";
        return View(t);
    }

Model:

public class Testing
{
    public int a = 0;
    public string name;
    public string age;
}

View:

@using(Html.BeginForm())
{
    @Html.AntiForgeryToken()

     @Html.Label("name")
     @Html.EditorFor(m => m.name)
     @Html.Label("age")
     @Html.EditorFor(m => m.age)    
    <button type="submit" value="Change Text" />
}

and this method get model back

    [HttpPost]
    [ActionName("Test")]
    [ValidateAntiForgeryToken]
    public ActionResult Test_post(Testing t)
    {
        Testing get_t = new Models.Testing();
        get_t = t;
        return View();
    }

but it returns null model please help me this is just demo I am stuck in my project.

ssilas777
  • 9,672
  • 4
  • 45
  • 68
Hamza Zaidi
  • 401
  • 7
  • 15
  • 1
    Your class `Testing` contains only fields. Make them properties i.e `public int a { get; set; }` so the `DefaultModelBinder` can **set** them. –  Sep 02 '15 at 11:38
  • Oh what a small mistake.. thank you soooo much you make my day :-) – Hamza Zaidi Sep 02 '15 at 11:41

1 Answers1

0

Update your mode to use properties, then only MVC model binding will work

public class Testing
{
    public int a = 0;
    public string name { get; set;} 
    public string age { get; set;} 
}
ssilas777
  • 9,672
  • 4
  • 45
  • 68