0

My Razor web page MVC 4.7 contains a form.

Pressing submit triggers a javascript function which validates the input data and then calls,

$("#form").submit();

My server-side HttpPost function uses the form data to insert a new record into a database.

It then updates a variable contained in the model to persist the new record's Id which I need back in the view.

My controller method looks something like this:

[HttpPost]    
public ActionResult SaveEnlevement(FormModel model)
{
   var item = BllClass.AddNewRecord(FormModel);

   model.Id = item.Id;

   return View("formPartial", model);
}

My page view contains:

@Html.HiddenFor(m => m.Id, new { @id = "ItemId" })

The code immediately following my submit goes like this.

$("#form").submit(); 
var NewIdOfItem = $("#ItemId").val()

I need to retrieve the Id of the new record and yet, once the form has submitted, $("#ItemId").val() is empty.

My presumption is that subsequent js needs to be contained in a sort of callback?

What would be the most effective way of getting the Id of my new record following form postback?

Scribbio
  • 113
  • 8

0 Answers0