I've inherited my first MVC project and it involves using MVC3 on top of Linq to SQL. I've been trying to find a way to generate a check box list based on a many to many relationship involving a cross table.
I have a systemFailureType table that maps to a SystemFailureProblem table via a cross table.
Here is my designer layout for the tables:
And here are my models:
[MetadataType(typeof(SystemFailureProblemMetadata))]
public partial class SystemFailureProblem
{
private class SystemFailureProblemMetadata
{
public int ID { get; set; }
[Required]
[StringLength(200)]
[DisplayName("Problem Description")]
public String Description { get; set; }
public IList<xSystemFailureProblemToType> FailureTypeCategories { get; set; }
}
}
[MetadataType(typeof(SystemFailureTypeMetaData))]
public partial class SystemFailureType
{
private class SystemFailureTypeMetaData
{
public int ID { get; set; }
[Required]
[StringLength(200)]
public String Description { get; set; }
}
}
My current view code uses a view model that contains a problem object. So the current code I have for generating the checkbox list looks like this:
@for(int i=0;i < Model.problem.FailureTypeCategories.Count(); i++)
{
@Html.CheckBox("FailureTypeCategories["+i+"].ID", false)
}
My main issue is that I'm getting some errors when I try to generate the checkbox list saying that the FailureTypeCategories collection doesn't exist. I suspect it may be related to how I have the models set up currently. My initial thoughts are leaning towards implementing a model for the cross table, though I'm not quite sure how I would integrate that. Is there an different way I should be going about this, or am I on the right track and just missing something?
Edit:
Here is the ViewModel
public SystemFailureProblem problem { get; set; }
public SystemFailureProblemViewModel() { }
public SystemFailureProblemViewModel(SystemFailureProblem problem)
{
this.problem = problem;
}
The controller method is very simple. It just returns a partial view of the form.
public ActionResult Edit(int id)
{
try
{
return PartialView("Form", context.SystemFailureProblems.Single(p => p.ID == id));
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
return PartialView("Form", null);
}
}
SystemFailureTyperelated to theSystemFailureProblemthat is being edited?