1

Working on MVC 4 and Entity framework, facing a tricky problem after svind new record i've passed Id to view model but it's remaining same ;(:

public ActionResult Edit(SubscriptionViewModel oSubModel) {
    if (ModelState.IsValid) {
        Subscription oSubDetail = new Subscription();
        oSubDetail.Title = oSubModel.Title;
        oSubDetail = this.subRep.SaveSubscription(oSubDetail);
        if (oSubDetail == null) {
            ModelState.AddModelError("error", "Error in saving the data,please try after some time.");
        } else {
            oSubModel.Id = oSubDetail.Id; //Id return to view
            ModelState.AddModelError("success", "User saved");
        }
    }
    }
    return View(oSubModel); //Model return to view 
    // Note: id that I've passed display here correct but in view its still 0
}


@using (Html.BeginForm()) {
    <div class="row">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
        @Html.HiddenFor(model => model.Id)
        <div class="col-md-6">
            <div class="form-group">
                @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label " })
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
                <a href="@Url.Action(" Subscription ", "Subscription ")" class="btn btn-default">
                        Back to Subscriptions
                </a>
            </div>
        </div>
    </div>
}

Note: In Controller id that I've passed display correct but in view its still 0

Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
  • Thats because the `HtmlHelper` methods use the values fro `ModelState` if they exists (which in your case they do). The behavior is explained in [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111). but you should not be returning the view once the data is saved. You should be redirecting (the PRG pattern) –  Jun 23 '17 at 07:22

1 Answers1

0

The ID is not changing because MVC looks in the ModelState for the value that was originally served to the view. In order to prevent this, you need to remove the value in the ModelState:

oSubModel.Id = oSubDetail.Id; //Id return to view
ModelState.Remove("Id");

Now that's solved, when you save the record the first time, you're returning the ID. However, when you submit again, you're not using the ID and so it creates another record. Assuming your SaveSubscription function detects the presence of an ID and updates the record instead of inserting a new one, you need to pass it the ID:

Subscription oSubDetail = new Subscription();
oSubDetail.Title = oSubModel.Title;
oSubDetail.Id = oSubModel.Id;
oSubDetail = this.subRep.SaveSubscription(oSubDetail);
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55