Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm an MVC Newbie. I realize this is very basic stuff.

During entry of a Master Record, how do I get my MVC Controler action to redirect to a View for a secondary file that holds multiple records linked to the master record.

The scaffolding provides a view and controller to enter the master record but then takes you back to the Index View. I have tried various things in the controller to do this, but I'm striking out.

The tables are very simple and hold people and charges. One person, many charges.

I need to...

1. Enter new person record

2. Click Save

3. Navigate to view that allows entry of one or many charges that will be linked to the person.

Here is my Post in the controller.

C#
[HttpPost]
public ActionResult Create(dataOffender offender)
{
    if (ModelState.IsValid)
    {
        db.dataOffenders.Add(offender);
        db.SaveChanges();
        return RedirectToAction("Index");

        <<<<<<<<<TODO:redirect to my charges "CreateCharge" view. 

    }

    return View(offender);
}



Thanks for any help you are willing to give.
Posted

1 solution

There are a number of approaches that you can take. I'm going to show the POCO route, since that's how I approach MVC.

First I would make sure that the dataOffender class has a 1:M link to the charges object:
C#
public class dataOffender
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
    public int Id {get; set; }

   /* other fields */

   public virtual ICollection<charge> Charges { get; set; }
</charge>


I would make sure that the foreign key relationship is defined in the charges class:
C#
public class charge
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
    public int Id { get; set; }

    [Display(AutoGenerateField = false), ForeignKey("dataOffender")]
    public int OffenderId { get; set; }

    /* other fields */

   public virtual dataOffender Offender { get; set; }

    public charge() {}

    public charge(int offenderId)
    {
         OffenderId = offenderId;
    }
}


This will establish your relationship and allow all that tasty referential integrity. Now on your controller you'll want to add an action that will allow you to create charges.
C#
OffenderController.cs
[HttpPost]
        public ActionResult Create(dataOffender offender)
        {
            if (ModelState.IsValid)
            {
                db.dataOffenders.Add(offender);
                db.SaveChanges();
                TempData["offenderId"] = offender.Id;
                return RedirectToAction("CreateCharge","Charge");                            
            }
 
            return View(offender);
        }

ChargeController.cs
public ActionResult CreateCharge()
{
    return View((TempData["offenderId"] != null) ? new charge(TempData["offenderId"]) : new charge());
}

[HttpPost]
public ActionResult CreateCharge(charge new_charge)
{
    if(ModelState.IsValid)
    {
        db.charges.Add(new_charge);
        db.SaveChages();
        return RedirectToAction("Index");
    }
}


I just threw this together without testing, so I apologize for any errors.
 
Share this answer
 
Comments
Vondersmith 11-Jul-14 9:00am    
That looks very straightforward. I like the overloaded function and the redirect to action. The POCO is new to me. I will need to learn more about it. I'm using EF and Database First. So should I modify my Generated version of the Charge?
Nathan Minier 29-Jul-14 10:26am    
Sorry, I've been on the road for a bit. If you're having any issues on the DB-first side please let me know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900