-1

Fairly new to MVC so I might be missing something

I have a simple view which is for inserting or updating a model.

The model class:

public class UserModel
{
    public Guid UserID { get; set; }

    [Required(ErrorMessage="Required")]
    [DisplayName("Email Address")]
    public string EmailAddress { get; set; }

    [Required(ErrorMessage = "Required")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required(ErrorMessage = "Required")]
    [DisplayName("Security Token")]
    public string SecurityToken { get; set; }
}

Assume the user has not submitted this form - I include the UserID as a hidden form element. Here is my view

@using (Html.BeginForm("Configure", "Home"))
{
    @Html.HiddenFor(Model => Model.UserID)
    @Html.LabelFor(Model => Model.EmailAddress) 
    @Html.TextBoxFor(Model => Model.EmailAddress) @Html.ValidationMessageFor(m => m.EmailAddress)
    @Html.LabelFor(Model => Model.Password)
    @Html.EditorFor(Model => Model.Password) @Html.ValidationMessageFor(m => m.Password)
    @Html.LabelFor(Model => Model.SecurityToken)
    @Html.TextBoxFor(Model => Model.SecurityToken) @Html.ValidationMessageFor(m => m.SecurityToken)
    <p><input type="submit" id="setupSalesforce" value="Save" /></p>
}

In my business layer classes I check if the Model.UserID is set, if it is then this is an update, if it is not set, this is an insert.

My issue is that after I have inserted data, when the view is generated again, the hidden field is not updated with the Guid in model.UserID , so it stays as a default Guid and so if I wanted to update that record I cannot as the Guid is not set.

I have stepped through my Controller method

[HttpPost]
public ActionResult Configure(Models.SalesforceUserModel model)
{
    var businessLayerFunctions = new BL.Functions();
    bool success = businessLayerFunctions.InsertOrUpdateUser(model);
    return View(model);
}

And the model has the UserID set. It just will not be written to the hidden element.

Here is the get method - but this isn't run after the save (I believe)

public ActionResult Configure()
{
    var businessLayerFunctions = new BL.Functions();
    var model = businessLayerFunctions.GetUserModel();
    return View(model);
}
andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Are you updating the `model` object inside `InsertOrUpdateUser` ? Also you should consider [PRG pattern](http://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction/11209320#11209320) after successful save. – Shyju Dec 15 '16 at 20:18
  • Can you post the Get method for that view? – TheFallenOne Dec 15 '16 at 20:18
  • Your `InsertOrUpdateUser` only returns `success` so the new value is never known. You could change the `InsertOrUpdateUser` to return the `SalesforceUserModel` object, updated. Or, since it is passed by reference, just update it in the `InsertOrUpdateUser` function. – nurdyguy Dec 15 '16 at 20:22
  • I am updating the model in InsertOrUpdateUser – andrewb Dec 15 '16 at 20:24
  • Attach the debugger so you can watch the `model` as you pass through/over the `InsertOrupdateUser` method. – nurdyguy Dec 15 '16 at 20:30
  • model IS being written to in InsertOrUpdateUser. The issue is in the UI, the hidden field is not set to model.UserID (or rather, model.UserID is still a default Guid) – andrewb Dec 15 '16 at 20:31
  • As a comment, how do you get that particular user ID in the GetUserModel() without passing the ID parameter? – TheFallenOne Dec 15 '16 at 20:46
  • @The_Outsider I pass it into the Business Layer constructor. Removed it for brevity – andrewb Dec 15 '16 at 20:56
  • Your returning a **new** view with new values, so do this correctly and follow the PRG pattern and redirect. But an option is to use `ModelState.Clear();` The behavior you getting is described in [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Dec 15 '16 at 21:14

2 Answers2

2

This might help you the result of which I found here Html.Hidden field not getting set.

Change your POST method to

[HttpPost]
public ActionResult Configure(Models.SalesforceUserModel model)
{
    var businessLayerFunctions = new BL.Functions();
    bool success = businessLayerFunctions.InsertOrUpdateUser(model);
    ModelState.Remove("UserID")
    return View(model);
}

You might have to populate the model again after ModelState.Remove

Community
  • 1
  • 1
TheFallenOne
  • 1,598
  • 2
  • 23
  • 57
-1

Is the first line in your view declaring the model? Should be something like:

@model mynamespace.UserModel
codestever
  • 79
  • 4