Redirect Url After Form Submission

ChaseHardin

I have a basic form that allows a user to create an Employee. Once the user selects the submit button, the page should be redirected to a summary page. This summary page needs to show the details that were added on the Employee form.

For example, I have a form that looks like this:

enter image description here

Once the user selects Submit, it should then redirect the user to the following page:

enter image description here

The issue that I'm running into is getting that redirect url. My url looks something like: http://www.localhost.com/Employee/1234/ The 1234 represents the Employee id. How can I pull that Employee Id?

My Current HTML

<button type="submit" name="saveButton" id="saveBtn" class="btn btn-primary">Submit</button>

Please note that I am using .NET, so am open to using Razor Syntax.

Update on Story

After reading all of your comments, I went and dug deeper into my problem. This is what I found.

There is a RouteConfig.cs file with the following route:

routes.MapRoute(
  name: "ListEmployee",
  url: "Employee/{employeeId}",
  defaults: new { controller = "Employee", action = "List"},
  constraints: new {employeeId = "\\d+"}
)

Because I have this route, I decided to modify this controller method to utilize it:

[HttpPost]
public ActionResult AddEmployee(ViewModel<EmployeeModel> model)
{
  // logic here

  return RedirectToAction("ListEmployee", new { employeeId = model.Body.Employee.Id, fromEmployeeProject = true });
}

Issue: This completely crashes. Stating: No route in the route table matches the supplied values. Any ideas on what I am doing wrong?

trashr0x

If you're using Entity Framework (EF), it follows up each INSERT statement with SCOPE_IDENTITY() when auto-generated ids are used.

SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. (MSDN)

As such, if your Employee ID is an auto-generated column in the database, you should be able to get the ID right after saving your changes:

[HttpPost]
public ActionResult Add(Employee employee)
{
    using (var context = new MyContext())
    {
        // add the employee object
        context.Employees.Add(employee);
        // save changes
        context.SaveChanges();
        // the id will be available here
        int employeeId = employee.Id;   
        // ..so perform your Redirect to the appropriate action, _
        // and pass employee.Id as a parameter to the action       
        return RedirectToAction("Index", "Employee", new { id = employeeId });            
    }
}

Obviously you haven't provided us with your controller and action names so make sure that you replace them with the appropriate values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django redirect to index view with correct URL after form submission

From Dev

Redirect after AJAX form submission

From Dev

Cannot redirect after form submission

From Dev

redirect straight after form submission

From Dev

Redirect after AJAX form submission

From Dev

Run PHP Code After Form Submission And Redirect

From Dev

RoR: form_tag, redirect after submission

From Dev

Not able to redirect a Django form after submission

From Dev

How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

From Dev

How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

From Dev

Redirect to URL after form post

From Dev

Should I use a 301, 302, or 303 redirect after form submission?

From Dev

get redirect_to and flash notices to work after ajax form submission

From Dev

How to redirect to new html page after form submission in Apps script

From Dev

Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

From Dev

How to redirect page on form submission

From Dev

redirect url after selecting option in HTML form?

From Dev

Redirect to same page after successful submission of a form and clicking OK on a confirmation message

From Dev

How to redirect back to html after JSP Form submission (with success/error message)

From Dev

I am getting a Cannot modify header information when trying to redirect a user after form submission

From Dev

Redirect to same page after successful submission of a form and clicking OK on a confirmation message

From Dev

Keeping model after form submission

From Dev

Params are empty after form submission

From Dev

Form submission after Javascript Validation

From Dev

Returning null after form submission

From Dev

Reset My form after submission

From Dev

send email after form submission

From Dev

Close Window After Submission of Form

From Dev

Delay form submission after preventDefault()

Related Related

  1. 1

    Django redirect to index view with correct URL after form submission

  2. 2

    Redirect after AJAX form submission

  3. 3

    Cannot redirect after form submission

  4. 4

    redirect straight after form submission

  5. 5

    Redirect after AJAX form submission

  6. 6

    Run PHP Code After Form Submission And Redirect

  7. 7

    RoR: form_tag, redirect after submission

  8. 8

    Not able to redirect a Django form after submission

  9. 9

    How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

  10. 10

    How to redirect URL in CodeIgniter after form submission back to the previous page only when the user comes from that page?

  11. 11

    Redirect to URL after form post

  12. 12

    Should I use a 301, 302, or 303 redirect after form submission?

  13. 13

    get redirect_to and flash notices to work after ajax form submission

  14. 14

    How to redirect to new html page after form submission in Apps script

  15. 15

    Trigger a redirect to a page on form submission after three incorrect attempts in Javascript

  16. 16

    How to redirect page on form submission

  17. 17

    redirect url after selecting option in HTML form?

  18. 18

    Redirect to same page after successful submission of a form and clicking OK on a confirmation message

  19. 19

    How to redirect back to html after JSP Form submission (with success/error message)

  20. 20

    I am getting a Cannot modify header information when trying to redirect a user after form submission

  21. 21

    Redirect to same page after successful submission of a form and clicking OK on a confirmation message

  22. 22

    Keeping model after form submission

  23. 23

    Params are empty after form submission

  24. 24

    Form submission after Javascript Validation

  25. 25

    Returning null after form submission

  26. 26

    Reset My form after submission

  27. 27

    send email after form submission

  28. 28

    Close Window After Submission of Form

  29. 29

    Delay form submission after preventDefault()

HotTag

Archive