1

This is my View:

@using (Html.BeginForm("Save", "Test", FormMethod.Post))
{
<label for="txtFirstName">First Name</label>
<input id="txtFirstName" type="text" />

<label for="txtLastName">Last Name</label>
<input id="txtLastName" type="text" />

<label for="txtUsername">Username</label>
<input id="txtUsername" type="text" />

<label for="txtEmail">Email</label>
<input id="txtEmail" type="text" />

<input type="submit" value="Save"/>
}

This is my ActionMethod:

public RedirectToRouteResult Save()
{
    var user = new User();
    TryUpdateModel(user);
    Database.SaveEntity(user);
    return RedirectToAction("Index");
}

This is my Model: @model Game.Model.User

When I debug and step-over TryUpdateModel, the user object doesn't update to the values I entered in the View.

Can anyone see where I am going wrong?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Subby
  • 5,370
  • 15
  • 70
  • 125
  • Check [HERE](http://stackoverflow.com/questions/7735635/real-example-of-tryupdatemodel-asp-net-mvc-3) TryUpdateModel is explicit model binding. I think your codes are correct. Try `db.SaveChanges();` instead of `Database.SaveEntity();` – AliRıza Adıyahşi May 22 '13 at 19:50

2 Answers2

2

You're not receiving the form data that was post-backed.

Try this:

public RedirectToRouteResult Save(string txtFirstName, string txtLastName,
                                  string txtUsername, string txtEmail)
{
    var User = new User();

    user.FirstName = txtFirstName;
    user.LastName = txtLastName;
    user.Username = txtUsername;
    user.Email = txtEmail;

    TryUpdateModel(user);
    Database.SaveEntity(user);
    return RedirectToAction("Index");
}

To use a strongly typed view, do this:

@model Game.Model.User

@using (Html.BeginForm("Save", "Test", FormMethod.Post))
{
    @Html.LabelFor(m => m.FirstName)
    @Html.EditorFor(m => m.FirstName)

    @Html.LabelFor(m => m.LastName)
    @Html.EditorFor(m => m.LastName)

    @Html.LabelFor(m => m.Username)
    @Html.EditorFor(m => m.Username)

    @Html.LabelFor(m => m.Email)
    @Html.EditorFor(m => m.Email)

    <input type="submit" value="Save"/>
}

and then change your action method signature to receive a User:

public RedirectToRouteResult Save(User user)
{
    TryUpdateModel(user);

    Database.SaveEntity(user);

    return RedirectToAction("Index");
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • But how does user in the parameter know what it is? Or how do I send the player object across? – Subby May 22 '13 at 19:33
  • 1
    Well, I suppose your clicking a `Save` button in the User create/update view... the `@model` of this view is of type `User` and so when you postback the form, ASP.NET Model Binder will do its magic and databind the values to a `User` object (the parameter) of the `Save` action method. – Leniel Maccaferri May 22 '13 at 19:36
  • 1
    Oh... sorry... I see that you're not using a strongly typed view. In this case you have to do what I show in my updated answer. – Leniel Maccaferri May 22 '13 at 19:40
  • I added the user parameter but it still comes out empty after TryUpdateModel – Subby May 22 '13 at 19:40
  • At the top of the view, I have got: @model Game.Model.User – Subby May 22 '13 at 19:41
  • Oh, it's too ugly to pass all those parameters in. How do I make TryUpdateModel work instead of doing the above? – Subby May 22 '13 at 19:43
  • I have tried: HttpContext.Current.Application[user.Username] = DateTime.Now().ToString(); but it says "Current" doesn't exist under HttpContext. – Subby May 22 '13 at 21:30
  • You must have a `HttpContext` available. Probably it's null. Why are you doing this? You can use `ViewBag.DateTimeNow = DateTime.Now().ToString();` on the controller side and access it on the view side using `@ViewBag.DateTimeNow`. – Leniel Maccaferri May 22 '13 at 21:35
  • In the first part of ur answer, why do we need `TryUpdateModel(user);`? – gmail user Jan 08 '14 at 18:58
  • But you already assigned the properties to `user`. As far as, I know `TryUpdateModel` is only needed to update `model` from posted values. I'm little confused – gmail user Jan 09 '14 at 01:50
1

On View:

<input id="txtFirstName" type="text" />

I think you should add name property

<input id="txtFirstName" type="text" name="FirstName" />

or a simple way:

@Html.TextBoxFor(m=>m.FirstName)
Hau Le
  • 667
  • 2
  • 17
  • 42