views:

841

answers:

4

In my Controller in a Asp.net MVC 1 app I want to use UpdateModel to populate a variable with POST data in my controller. I've looked at dozens of examples but even the most basic ones seem to fail silently for me.

Here's a very basic example that's just not working. What am I doing wrong?

    public class TestInfo
    {
        public string username;
        public string email;
    }

   public class AdminController : Controller
    {

        public ActionResult TestSubmit()
        {
            var test = new TestInfo();
            UpdateModel(test);//all the properties are still null after this executes  
            //TryUpdateModel(test); //this returns true but fields / properties all null
            return Json(test);
        }


    }


//Form Code that generates the POST data
    <form action="/Admin/TestSubmit" method="post">
        <div>
            <fieldset>
                <legend>Account Information</legend>
                <p>
                    <label for="username">Username:</label>
                    <input id="username" name="username" type="text" value="" />
                </p>
                <p>
                    <label for="email">Email:</label>
                    <input id="email" name="email" type="text" value="" />
                </p>
                <p>
                    <input type="submit" value="Login" />
                </p>

            </fieldset>
        </div>
    </form>
+3  A: 

It looks like you're trying to get the controller to update the model based on the form elements. Try this instead:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult TestSubmit(TestInfo test)
    {
        UpdateModel(test);
        return Json(test);
    }

In your code, you're creating a new TestModel instead of letting the MVC runtime serialize it from the HttpPost. I've let myself get wrapped around the axel on this also, you're not the only one!

JMP
This is good practice but using fields instead of properties was what caused my assignments to fail silently.
Glenn
A: 

I'm not too familiar with ASP.NET MVC, but shouldn't your TestSubmit method look more like this:

public ActionResult TestSubmit(TestInfo test)
{
  UpdateModel(test);
  return Json(test);
}
Charlie
+2  A: 

make properties of your public field:

    public class TestInfo
    {
        public string username {get;set;}
        public string email{get;set;}
    }
Gregoire
UpdateModel relies on properties, not fields, iirc.
Chad Ruppert
ahh missed that. better practice anyway I guess
Glenn
Odd that it doesn't work for fields. Guess that's my punishment for not doing the extra coding required for setting property defaults in the constructor :-)
Glenn
A: 

In the controller you should have two methods, one to respond to the GET, the other, if required is for responding to the POST.

So, firstly have a GET method:

public ActionResult Test ()
{
    return View (/* add a TestInfo instance here if you're getting it from somewhere - db etc */);
}

Secondly, you'll need a POST method:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test (TestInfo test)
{
    return Json (test);
}

Notice that there's no UpdateMethod there, the ModelBinder would have done that for you.

Kieron
These are good best practices but my problem was using fields. I tested your suggestion and it a clean way to split get and post requests
Glenn