Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I redirect to a page with an ID that doesn't exist yet?
    primarykey
    data
    text
    <p>Basically put, I have an action that saves a form to a database, then redirects the user to a list. From this list, the user can select a few actions.</p> <p>One of these actions is essential and I would like the user to be passes directly to this straight after creating an object, however, as the ID of the first object does not exist yet, I am not sure how to pass the ID through.</p> <p>For example Currently - </p> <pre><code> //Excerpt db.form.Add(ViewModel); db.SaveChanges(); return RedirectToAction("List"); </code></pre> <p>Then I have a list that is pulled in the normal way, and there is a link that the user can click to go to <code>/stage2/(id)</code>.</p> <p>What I would ideally like is to change my code to something like:</p> <pre><code> //Excerpt db.form.Add(ViewModel); var newentry = db.SaveChanges(); return RedirectToAction("Stage2", new {id = newentry)); </code></pre> <p>I have seen that calling <code>SaveChanges()</code> in this manner results an int being returned, but, I have tried and it doesn't seem to pull the ID of the object, I have tried a few other things but am unsure of the syntax to use.</p> <p>All I can think is to detach the object, then select it again and return, but, is there a more "slick" or direct way of doing this?</p> <hr> <hr> <p>EDIT - I think I have worked this out... but, I need to wait 7 hours until I can post an answer.</p> <p>Unless I am mistaken - in which case, please can someone tell me!... It looks like that Entity Framework automatically updates the object that you pass it.</p> <p>When I pass it a <code>form</code> object, upon calling <code>SaveChanges</code>, it automatically sets the ID of the form object.</p> <p>So, all I needed was:</p> <pre><code> //Excerpt db.form.Add(ViewModel); return RedirectToAction("Stage2", new {id = ViewModel.id)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload