This is driving me nuts. I'm trying to edit a many-to-many relation, following this nice answer, but as I added a second relation, things started to get funky.
public ActionResult Edit(Style style)
{
var accessoryIDs = style.SelectedAccessories.Select(a => a.ID);
var fabricIDs = style.SelectedFabrics.Select(f => f.ID);
When loading style
for editing in the first place, both lists are populated and all is good. So I make my edits and save.. style.SelectedAccesories
is fine, style.SelectedFabrics
is null!
In my view, I have the editorfor accessories above the one for fabrics. If I swap the two, so that fabrics come before accessories, accessories is null where fabrics isn't.
I started out with a shared model for the two Selected
s and dublicated to make sure it didn't have something to with that (see how desperate I am?) which of course it didn't.
I'm thinking that I'm missing something ridiculous..
Feel free to ask for more code if you need it.
Edit: I realize that I've asked an off-topic question and am sorry but I was really frustrated and hoped someone could look at the outline and say "Oh, that, you just need to tweak that one there and it'll be all good". I guess it runs deeper than that, so here's code to add to make a proper question:
Controller actions:
public ActionResult Edit(int? id)
{
var style = db.Styles.Include(s => s.Fabrics).Include(s => s.Accessories).FirstOrDefault(s => s.ID == id);
style.SelectedAccessories = db.Accessories.Select(f => new SelectedAccessory { ID = f.ID, Text = f.Name }).ToList();
style.SelectedFabrics = db.Fabrics.Select(f => new SelectedFabric { ID = f.ID, Text = f.Name }).ToList();
return View(style); // As said, the two above aren't null at this point
}
[HttpPost]
public ActionResult Edit(Style style)
{
var accessoryIDs = style.SelectedAccessories.Where(s => s.IsSelected).Select(s => s.ID);
// This one (style.SelectedFabrics) is now null
var fabricIDs = style.SelectedFabrics.Where(s => s.IsSelected).Select(s => s.ID);
return View(style);
}
Edit view:
@model Styleinfo.Models.Style
@using (Html.BeginForm())
{
<div class="form-horizontal">
@*swapping around makes the last of the two null in the controller*@
@Html.EditorFor(model => model.SelectedAccessories)
@Html.EditorFor(model => model.SelectedFabrics)
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
Editor view (SelectedFabric
for fabrics, they're otherwise identical as mentioned):
@model Styleinfo.Models.SelectedAccessory
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.ID)
@Html.LabelFor(model => model.IsSelected, Model.Text)
@Html.EditorFor(model => model.IsSelected)
}
The models:
public partial class Style
{
public IEnumerable<SelectedFabric> SelectedFabrics { get; set; }
public IEnumerable<SelectedAccessory> SelectedAccessories { get; set; }
}
public class SelectedAccessory // Again, SelectedFabric, otherwise identical
{
public int ID { get; set; }
public string Text { get; set; }
public bool IsSelected { get; set; }
}
T-SQL to create the simplified database and consequently the rest of Style
:
-- Creating table 'Fabrics'
CREATE TABLE [dbo].[Fabrics] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
);
GO
-- Creating table 'Styles'
CREATE TABLE [dbo].[Styles] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
);
GO
-- Creating table 'Accessories'
CREATE TABLE [dbo].[Accessories] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(max) NOT NULL,
);
GO
-- Creating table 'FabricStyle'
CREATE TABLE [dbo].[FabricStyle] (
[Fabrics_ID] int NOT NULL,
[Styles_ID] int NOT NULL
);
GO
-- Creating table 'AccessoryStyle'
CREATE TABLE [dbo].[AccessoryStyle] (
[Accessories_ID] int NOT NULL,
[Styles_ID] int NOT NULL
);
GO