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);
}