0

I'm working with ASP.NET MVC, and I'm trying to make changes to the view model after the user has posted it to the server and then send those changes back down to the view.

However, the changes aren't going back to the view except where I'm using Model.<property> in the Form. The bound Html helpers retain whatever the user typed in. I know I'm missing something obvious, I just can't see it.

View

@model ACE.ViewModels.SecureVM

@using (Html.BeginForm(Model.ActionMethod, Model.Controller,     FormMethod.Post))
{ 
    <table>
        <tr>
            <td>System Name</td>
            <td>
                @Html.TextBoxFor(model => model.SystemName)  
            </td>
        </tr>
        <tr>
            <td>User Name</td>
            <td>@Html.TextBoxFor(model => model.UserName)</td>
        </tr>
        <tr>
            <td>User First Name</td>
            <td>@Html.TextBoxFor(model => model.UserFirstName)</td>
        </tr>
        <tr>
            <td>User Last Name</td>
            <td>@Html.TextBoxFor(model => model.UserLastName)</td>
        </tr>
    </table>

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

Controller

public ActionResult SecureLaunch()
{
    var model = new SecureVM();
    model.Controller = "LaunchAsCustomer";
    model.ActionMethod = "SecureLaunch";

    return this.View(model);
}


[HttpPost]
public ActionResult SecureLaunch(SecureVM model)
{
    model.UserName = "Bob120";
    model.UserFirstName = "Bob";
    model.UserLastName = "Jones";
    model.Controller = "Test";
    model.ActionMethod = "Test";

    return this.View(model);
}

This is an example scenario that demonstrates the problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
Danny Ellis Jr.
  • 1,674
  • 2
  • 23
  • 38
  • 1
    If you want to display a different view, the follow the PRG pattern and redirect (`return View()` is for returning the view as posted, not a new view). The behavior is explained in the 2nd part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Sep 16 '16 at 12:29
  • I'm not sure I understand how it is a "new" view. It's the same view just with different data. I'm pretty certain I've done this in other places before and didn't have this problem. Thanks though, that answer has given me more to explore. – Danny Ellis Jr. Sep 16 '16 at 12:38
  • I had to clear and rebuild the model. That did it. If you make your comment into an answer, I'll accept it. Thanks for the help. – Danny Ellis Jr. Sep 16 '16 at 13:36

0 Answers0