Redirect with different id after create MVC 4

dasdasd

I have a model classroom that have an array of enrollments that have an array of components and components have an array of subcomponents. (I know it can be confusing and maybe not the best way)

public class Classroom
    {
        ...
        public virtual List<Enrollment> Enrollments { get; set; }
}
public class Enrollment
    {
       ...
       public virtual int ClassroomId { get; set; }
        public virtual Classroom Classroom { get; set; }
        public virtual List<Components> Components { get; set; }
       }
public class Component
    {
       ...
        public virtual int EnrollmentId { get; set; }   
        public virtual Enrollment Enrollment{ get; set; }
        public virtual List<Subcomponent> Subcomponents { get; set; }
}
public class Subcomponent
    {
       ...
        public virtual int ComponentId { get; set; }   
        public virtual Component Component{ get; set; }
}

In a view on the enrollment i have a table to show the components and in the future the subcomponents. After creating a subcomponent i need to redirect to that enrollment view with the respective enrollment id. I have this code:

public ActionResult CreateSubcomponent(Subcomponent subcomponent)
        {
            if (ModelState.IsValid)
            {
                db.Subcomponents.Add(subcomponent);
                db.SaveChanges();
                return RedirectToAction("View", "Enrollment", new { id = ??? });
            }
...
}

My question is how can i do it? How can i get the id from the enrollment? Do i need to change the code, and if so, how? Thanks

Shyju

To save a SubComponent, you need to save the ComponentId because it is a foreign key. That means you should have the ComponentId in your action method. You should query the database to get the Component record for that ComponentId and use the Enrollement property of that entity.

public ActionResult CreateSubcomponent(Subcomponent subcomponent)
{
   if (ModelState.IsValid)
   {
       db.Subcomponents.Add(subcomponent);
       db.SaveChanges();

       var c=db.Components.FirstOrDefault(s=>s.Id=subComponent.ComponentId);
       var enrollmentId = c.EnrollementId;
       return RedirectToAction("View", "Enrollment", new { id = enrollmentId });
    }
    // to do : return something
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to Redirect to different page on click ? mvc4

From Dev

Redirect to specific page after session expires (MVC4)

From Dev

IdentityServer4 Redirect to different client after login

From Dev

Dropnet on MVC after redirect

From Dev

MVC Redirect after login

From Dev

MVC4 Generating Create Form with different view model

From Dev

Different between id and name property in MVC 4 for jQuery?

From Dev

ASP.NET MVC 4 - Redirect to the same page after controller ends

From Dev

asp.net MVC 4 redirect to details after creating a new item in database?

From Dev

Create MVC 4 application

From Dev

How to redirect after angular function by mvc action

From Dev

Tuple in MVC is getting null after redirect

From Dev

Redirect after login in mvc5

From Dev

Redirect after POST method in spring MVC

From Dev

How to Redirect to a different page after login

From Dev

MVC 4 Filter result - redirect or view?

From Dev

MVC 4 Redirect from login page if authenticated

From Dev

MVC 4 Redirect when session ends

From Dev

Different landing page with MVC 4

From Dev

Log in with different browsers MVC 4

From Dev

Different landing page with MVC 4

From Dev

laravel 5.2 redirect after registration by id

From Dev

after using clone function in a row how can create different id for each row

From Dev

Redirect page after post request [Express 4]

From Dev

Rails 4 - redirect to correct url after submit

From Dev

Rails 4 - redirect to correct url after submit

From Dev

How to Pass the Email Id value after checking Captcha in Asp.Net Mvc4?

From Dev

rails 4 instance variable id changes to hash after new and before create

From Dev

MVC Website looks different after publishing it

Related Related

HotTag

Archive