• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1496
  • Last Modified:

MVC viewmodel formcollection becomes null for some queries on POST

Hey guys, i've been stumped on this one awhile, rewritten several times, googled to death... MVC is driving me nuts.

For this question I have a database with 3 tables, each a 1 to many relationship to the other,
One Control with Many Procedures, each Procedure has Many Objectives.  
tableControl relates to tableProcedures via a common ControlID
tableObjective relates to tableProcedures via a common ProcedureID

My Controller looks like this:
 [Authorize]
        public ActionResult EditControl(string controlID)
        {
           MyProg.ViewModels.EditControl editControl = new MyProg.ViewModels.EditControl()
            {
                tableControl = repo.GetControl(controlID),
                tableProcedure = repo.ViewProcedureDetails(controlID),
                tableObjective = repo.ViewObjectiveDetails(controlID)
            };
return View(editControl);
}

 [HttpPost]
        public ActionResult EditControl(string controlID, MyProg.ViewModels.EditControl collection)
        { // UpdateModel logic is here 
At this point if I set a breakpoint collection states null for everything but my tableControl.  It seems that is because it is a standard query with just a single table.  Everything works fine if I make the Procedure return only one table but I want to make a single database call which contains all Procedures associated with the controlID have user edit them in the View and on Post save changes to database. 
}

Open in new window


Here is repository, also note I've tried different queries but have not had success except when requesting a single record. Everything displays and is in the collection during the initial Get View
public tableControl GetControl(string controlID) // works
        {
            return db.tableControls.SingleOrDefault(a => a.ControlID == controlID);
        }
public List<tableProcedure> ViewProcedureDetails(string controlID)
        {
            return (from procedures in db.tableProcedures
                    where procedures.ControlID == controlID
                    select procedures).ToList();
        }
public IQueryable<tableProcedure> GetProcedures(string controlID)
        {
            return from procedures in db.tableProcedures
                   where procedures.ControlID == controlID
                   select procedures;
        }
public IEnumerable<tableObjective> ViewObjectiveDetails(string controlID)
        {
            return from objectives in db.tableObjectives
                   join procedures in db.tableProcedures on objectives.ProcedureID equals procedures.ProcedureID
                   where procedures.ControlID == controlID
                   select objectives;
        }

Open in new window


lastly, here's my viewmodel class, tried different ones here as well:
public class EditControl
    {
 public IEnumerable<tableProcedure> ProcedureList { get; set; }
 public tableControl tableControl { get; set; }
 public List<tableProcedure> tableProcedure { get; set; } 
 public IEnumerable<tableControl> editingControl { get; set; }
 public IEnumerable<tableObjective> tableObjective { get; set; }
    }

Open in new window


Like I said, the Get looks good and a breakpoint shows tableProcedure is loaded in with [2] objects, I can access the data, but once the form is submitted and Posted, everything becomes null except tableControl.  Please let me know what I'm doing wrong here so it will no longer be null on Post, Linq to Sql  Thanks!
0
DigitalTechy
Asked:
DigitalTechy
  • 2
1 Solution
 
DigitalTechyAuthor Commented:
Thanks for expanding it.  In the mean-time I believe I may have found a solution.  Finally I found a website that addresses the issue, for others that are looking: http://www.mikesdotnetting.com/Article/105/ASP.NET-MVC-Partial-Views-and-Strongly-Typed-Custom-ViewModels

This has a one to many relationship with both partial views and strongly typed custom view models.  
0
 
DigitalTechyAuthor Commented:
No one else posted any possible solutions.  I finally found a website that had a potential workaround and it looks like it is the best solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now