Passing the “owner”-identity to the create-view for a related model












0














I have this viewmodel for Employees:



public class EmployeeViewModel
{
public int Id { get; set; }
// some more properties
public List<EmployeeReservationViewModel> Reservations { get; set; }
}


... and this one for EmployeeReservations:



public class EmployeeReservationViewModel
{
public int Id { get; set; }
public int EmployeeId { get; set; }
// some more properties
public EmployeeViewModel Employee { get; set; }
}


In the Employees Details-view, I have a link for adding a new EmployeeReservation, like this:



<a asp-controller="EmployeeReservations" 
asp-action="Create">Add reservation</a>


How should I pass the EmployeeId to the EmployeeReservations/Create-View?



At the moment I'm using a session-variable, like this, in the Employees's Details-method in the controller:



HttpContext.Session.SetInt32("EmployeeId", employee.Id);


... and then retreiving it in the Create POST-method in the EmployeeReservations-controller:



employeeReservation.EmployeeId = HttpContext.Session.GetInt32("EmployeeId").Value;
db.Add(employeeReservation);
await db.SaveChangesAsync();


I have a feeling my method is not optimal. What if the session times out? Is there another, more fail-safe method of doing it?










share|improve this question






















  • docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
    – JohnB
    Nov 23 at 6:38
















0














I have this viewmodel for Employees:



public class EmployeeViewModel
{
public int Id { get; set; }
// some more properties
public List<EmployeeReservationViewModel> Reservations { get; set; }
}


... and this one for EmployeeReservations:



public class EmployeeReservationViewModel
{
public int Id { get; set; }
public int EmployeeId { get; set; }
// some more properties
public EmployeeViewModel Employee { get; set; }
}


In the Employees Details-view, I have a link for adding a new EmployeeReservation, like this:



<a asp-controller="EmployeeReservations" 
asp-action="Create">Add reservation</a>


How should I pass the EmployeeId to the EmployeeReservations/Create-View?



At the moment I'm using a session-variable, like this, in the Employees's Details-method in the controller:



HttpContext.Session.SetInt32("EmployeeId", employee.Id);


... and then retreiving it in the Create POST-method in the EmployeeReservations-controller:



employeeReservation.EmployeeId = HttpContext.Session.GetInt32("EmployeeId").Value;
db.Add(employeeReservation);
await db.SaveChangesAsync();


I have a feeling my method is not optimal. What if the session times out? Is there another, more fail-safe method of doing it?










share|improve this question






















  • docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
    – JohnB
    Nov 23 at 6:38














0












0








0







I have this viewmodel for Employees:



public class EmployeeViewModel
{
public int Id { get; set; }
// some more properties
public List<EmployeeReservationViewModel> Reservations { get; set; }
}


... and this one for EmployeeReservations:



public class EmployeeReservationViewModel
{
public int Id { get; set; }
public int EmployeeId { get; set; }
// some more properties
public EmployeeViewModel Employee { get; set; }
}


In the Employees Details-view, I have a link for adding a new EmployeeReservation, like this:



<a asp-controller="EmployeeReservations" 
asp-action="Create">Add reservation</a>


How should I pass the EmployeeId to the EmployeeReservations/Create-View?



At the moment I'm using a session-variable, like this, in the Employees's Details-method in the controller:



HttpContext.Session.SetInt32("EmployeeId", employee.Id);


... and then retreiving it in the Create POST-method in the EmployeeReservations-controller:



employeeReservation.EmployeeId = HttpContext.Session.GetInt32("EmployeeId").Value;
db.Add(employeeReservation);
await db.SaveChangesAsync();


I have a feeling my method is not optimal. What if the session times out? Is there another, more fail-safe method of doing it?










share|improve this question













I have this viewmodel for Employees:



public class EmployeeViewModel
{
public int Id { get; set; }
// some more properties
public List<EmployeeReservationViewModel> Reservations { get; set; }
}


... and this one for EmployeeReservations:



public class EmployeeReservationViewModel
{
public int Id { get; set; }
public int EmployeeId { get; set; }
// some more properties
public EmployeeViewModel Employee { get; set; }
}


In the Employees Details-view, I have a link for adding a new EmployeeReservation, like this:



<a asp-controller="EmployeeReservations" 
asp-action="Create">Add reservation</a>


How should I pass the EmployeeId to the EmployeeReservations/Create-View?



At the moment I'm using a session-variable, like this, in the Employees's Details-method in the controller:



HttpContext.Session.SetInt32("EmployeeId", employee.Id);


... and then retreiving it in the Create POST-method in the EmployeeReservations-controller:



employeeReservation.EmployeeId = HttpContext.Session.GetInt32("EmployeeId").Value;
db.Add(employeeReservation);
await db.SaveChangesAsync();


I have a feeling my method is not optimal. What if the session times out? Is there another, more fail-safe method of doing it?







c# asp.net-core-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 6:30









Stian

335213




335213












  • docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
    – JohnB
    Nov 23 at 6:38


















  • docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
    – JohnB
    Nov 23 at 6:38
















docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
– JohnB
Nov 23 at 6:38




docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
– JohnB
Nov 23 at 6:38












1 Answer
1






active

oldest

votes


















2














You can use asp-route-id



In view



<a asp-controller="EmployeeReservations" 
asp-action="Create" asp-route-id="10">Add reservation</a>


In Controller add id as parameter fore create action



Public IActionResult Create(string id){
}





share|improve this answer





















  • I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
    – Stian
    Nov 23 at 7:00










  • You can use it like this but good practice is passing the employee id in a model to view from create controller
    – Jeswin Rebil
    Nov 23 at 7:11










  • It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
    – Stian
    Nov 23 at 7:24










  • @Stian cheers... kindly mark as answer :)
    – Jeswin Rebil
    Nov 23 at 7:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441614%2fpassing-the-owner-identity-to-the-create-view-for-a-related-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use asp-route-id



In view



<a asp-controller="EmployeeReservations" 
asp-action="Create" asp-route-id="10">Add reservation</a>


In Controller add id as parameter fore create action



Public IActionResult Create(string id){
}





share|improve this answer





















  • I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
    – Stian
    Nov 23 at 7:00










  • You can use it like this but good practice is passing the employee id in a model to view from create controller
    – Jeswin Rebil
    Nov 23 at 7:11










  • It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
    – Stian
    Nov 23 at 7:24










  • @Stian cheers... kindly mark as answer :)
    – Jeswin Rebil
    Nov 23 at 7:25
















2














You can use asp-route-id



In view



<a asp-controller="EmployeeReservations" 
asp-action="Create" asp-route-id="10">Add reservation</a>


In Controller add id as parameter fore create action



Public IActionResult Create(string id){
}





share|improve this answer





















  • I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
    – Stian
    Nov 23 at 7:00










  • You can use it like this but good practice is passing the employee id in a model to view from create controller
    – Jeswin Rebil
    Nov 23 at 7:11










  • It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
    – Stian
    Nov 23 at 7:24










  • @Stian cheers... kindly mark as answer :)
    – Jeswin Rebil
    Nov 23 at 7:25














2












2








2






You can use asp-route-id



In view



<a asp-controller="EmployeeReservations" 
asp-action="Create" asp-route-id="10">Add reservation</a>


In Controller add id as parameter fore create action



Public IActionResult Create(string id){
}





share|improve this answer












You can use asp-route-id



In view



<a asp-controller="EmployeeReservations" 
asp-action="Create" asp-route-id="10">Add reservation</a>


In Controller add id as parameter fore create action



Public IActionResult Create(string id){
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 at 6:37









Jeswin Rebil

360514




360514












  • I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
    – Stian
    Nov 23 at 7:00










  • You can use it like this but good practice is passing the employee id in a model to view from create controller
    – Jeswin Rebil
    Nov 23 at 7:11










  • It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
    – Stian
    Nov 23 at 7:24










  • @Stian cheers... kindly mark as answer :)
    – Jeswin Rebil
    Nov 23 at 7:25


















  • I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
    – Stian
    Nov 23 at 7:00










  • You can use it like this but good practice is passing the employee id in a model to view from create controller
    – Jeswin Rebil
    Nov 23 at 7:11










  • It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
    – Stian
    Nov 23 at 7:24










  • @Stian cheers... kindly mark as answer :)
    – Jeswin Rebil
    Nov 23 at 7:25
















I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
– Stian
Nov 23 at 7:00




I have to add this in the Create-view to make that work: <input type="hidden" name="EmployeeId" value="@ViewContext.RouteData.Values["id"]" />. Am I right? Because I tested it, and it works. But it seems a bit "hacky" to have to add manual hidden inputs in the view.
– Stian
Nov 23 at 7:00












You can use it like this but good practice is passing the employee id in a model to view from create controller
– Jeswin Rebil
Nov 23 at 7:11




You can use it like this but good practice is passing the employee id in a model to view from create controller
– Jeswin Rebil
Nov 23 at 7:11












It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
– Stian
Nov 23 at 7:24




It worked when I added: EmployeeReservationViewModel vm = new EmployeeReservationViewModel { EmployeeId = id }; return View(vm); in the create GET method and <input type="hidden" asp-for="EmployeeId" /> in the create-view. :)
– Stian
Nov 23 at 7:24












@Stian cheers... kindly mark as answer :)
– Jeswin Rebil
Nov 23 at 7:25




@Stian cheers... kindly mark as answer :)
– Jeswin Rebil
Nov 23 at 7:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441614%2fpassing-the-owner-identity-to-the-create-view-for-a-related-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)