0

Hi this is more of an MVC question. I am implementing a save functionality in my MVC application. I have written a HTTPPOST method to save the records. When I am trying to pass the viewmodel to the view , I am amending one of the properties called requestID. This requestid is retrieved after i save a request. When my view loads I dont see the requestid field containing the data. I have bound the control correctly. Do you loose the viewmodel information during the post operation. What I am looking at is I need to refresh my view with the latest request that I have saved.

[HttpPost]
public ActionResult Request_Insert(NewRequestViewModel newRequestViewModel)
{

    newRequestViewModel.CompanyCode = "8100";
    newRequestViewModel.RequestStatusCode = "New";
    if (!ModelState.IsValid)
    {
        return null;
    }
    int requestId = requestRepository.CreateRequest(Mapper.Map<Request>(newRequestViewModel));

    newRequestViewModel.RequestID = requestId;


    return View("NewRequestView", Mapper.Map<NewRequestViewModel>(newRequestViewModel));
}

Requestid field

@model CC.GRP.MCRequest.ViewModels.NewRequestViewModel
     <div class="form-group">
                        @Html.LabelFor(model => model.RequestID, htmlAttributes: new { @class = "control-label col-md-4" })
                        <div class="col-md-8">
                            <div class="editor-field">
                                @Html.EditorFor(model => model.RequestID, new { htmlAttributes = new { @class = "form-control", style = "width:100%", @readonly = "readonly" } })

                            </div>
                            @Html.ValidationMessageFor(model => model.RequestID, "", new { @class = "text-danger" })
                        </div>
                    </div>
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
Tom
  • 8,175
  • 41
  • 136
  • 267
  • Can't tell from your 'Requestid Field' code, but I assume that the ViewModel is wrapped inside a Form that is sending the data to the controller via a post? – Matt Apr 13 '17 at 14:32
  • And have you checked that the object returned from Mapper.Map contains the new Id ? – Matt Apr 13 '17 at 14:34
  • I get the value of the requestid after executing CreateRequest method. I am doing a manual assignment.. When I check the newReqeustViewModel object value while debugging it contains the requestid – Tom Apr 13 '17 at 14:36
  • 1
    try ModelState.Clear(), http://stackoverflow.com/questions/787895/reset-the-value-of-textarea-after-form-submission – AGuyCalledGerald Apr 13 '17 at 14:45
  • where do I put it – Tom Apr 13 '17 at 14:54
  • 1
    It works . Thank you – Tom Apr 13 '17 at 14:56

0 Answers0