One product has been created, so the list shouldn't be empty. Here are the models:
public class Product
{
public int ProductID { get; set; }
public string ManufacturerCode { get; set; }
public int ManufacturerID { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductVariant> Variants { get; set; }
}
public class ProductVariant
{
[Key]
public int ProductVariantID { get; set; }
[ForeignKey("Product")]
public int ProductID { get; set; }
public virtual Product Product { get; set; }
public string Name { get; set; }
public string Color { get; set; }
Can you show complete sample code? I mean how the action look like, how the view look like.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ViewData["ProductID"] = new SelectList(_context.Products, "ProductID", "Product");
Just make it iewData["ProductID"] = new SelectList(_context.Products, "ProductID", "Name");
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Just make it iewData["ProductID"] = new SelectList(_context.Products, "ProductID", "Name");
The weird thing is that the templates don't automatically do this. Any idea on how to go about correcting this from a templating perspective, so I don't have to change it so often in the controllers?
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Member
1 Points
7 Posts
MVC template with select list from parent not working?
May 22, 2016 12:19 PM|Byatis|LINK
Hi!
I'm testing RC2 for my project, however can't seem to get default generated edit template work.
I have two models Product and ProductVariant. When trying to edit Variants through the generated code I've got error:
"NullReferenceException: Object reference not set to an instance of an object.
Microsoft.AspNetCore.Mvc.Rendering.MultiSelectList.Eval(Object container, String expression)"
One product has been created, so the list shouldn't be empty. Here are the models:
public class Product
{
public int ProductID { get; set; }
public string ManufacturerCode { get; set; }
public int ManufacturerID { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductVariant> Variants { get; set; }
}
public class ProductVariant
{
[Key]
public int ProductVariantID { get; set; }
[ForeignKey("Product")]
public int ProductID { get; set; }
public virtual Product Product { get; set; }
public string Name { get; set; }
public string Color { get; set; }
}
Any help would be appreciated. Thanks!
All-Star
33963 Points
8480 Posts
MVP
Re: MVC template with select list from parent not working?
May 22, 2016 01:04 PM|imran_ku07|LINK
Can you show complete sample code? I mean how the action look like, how the view look like.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Member
1 Points
7 Posts
Re: MVC template with select list from parent not working?
May 22, 2016 05:56 PM|Byatis|LINK
Generated code is here:
View crashes on this:
Project was updated from RC1 but these controllers and views were created by "Add scaffolded items"
All-Star
33963 Points
8480 Posts
MVP
Re: MVC template with select list from parent not working?
May 22, 2016 06:34 PM|imran_ku07|LINK
Just make it iewData["ProductID"] = new SelectList(_context.Products, "ProductID", "Name");
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Member
1 Points
7 Posts
Re: MVC template with select list from parent not working?
May 22, 2016 06:43 PM|Byatis|LINK
Thank you very much. I can't believe I didn't notice that.
Strange though that template generated it like this...
None
0 Points
3 Posts
Re: MVC template with select list from parent not working?
May 25, 2016 02:13 PM|Joshua.Hardy|LINK
The weird thing is that the templates don't automatically do this. Any idea on how to go about correcting this from a templating perspective, so I don't have to change it so often in the controllers?
All-Star
33963 Points
8480 Posts
MVP
Re: MVC template with select list from parent not working?
May 28, 2016 06:09 PM|imran_ku07|LINK
Can you log an issue at https://github.com/aspnet/Templates/?
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD