Error adding data to custom table from a custom form
Hi,
I'm new to Umbraco and really want to use it for a future project however I've hit a stumbling block which seems like a basic issue but I can't seem to get over.
I have a form within a partial view which is loaded within my page with a ViewModel which contains a select list and all the other relevant fields. When the form is loaded, I populate the select list from a custom table I've got which is a categories table. When I enter all the information, I suave it down and I then get a SqlException with the following error...
"Cannot insert the value NULL into column 'CategoryId', table 'UmbracoDb.dbo.cwFigure'; column does not allow nulls. INSERT fails."
When I debug the solution I follow the post to the controller and I have a CategoryId for that property in my model so it isn't null. I can't understand why it is falling over at this. Am I missing something basic?
Below are my models and the Save action from my controller.
FigureCategory.cs
namespace CW.Core.Model
{
[TableName("cwFigureCategory")]
[PrimaryKey("CategoryId", autoIncrement = true)]
[ExplicitColumns]
public class FigureCategory
{
[Column("CategoryId")]
[PrimaryKeyColumn(AutoIncrement = true)]
public int CategoryId { get; set; }
[Column("CategoryName")]
public string CategoryName { get; set; }
[Column("DateAdded")]
public DateTime DateAdded { get; set; }
[Column("AddedBy")]
public string AddedBy { get; set; }
}
}
Figure.cs
namespace CW.Core.Model
{
[TableName("cwFigure")]
[PrimaryKey("FigureId", autoIncrement = true)]
[ExplicitColumns]
public class Figure
{
[Column("FigureId")]
[PrimaryKeyColumn(AutoIncrement = true)]
public int FigureId { get; set; }
[Column("FigureName")]
public string FigureName { get; set; }
[Column("ReleaseYear")]
public string ReleaseYear { get; set; }
[ForeignKey(typeof(FigureCategory), Name = "FK_CategoryId_cwFigure")]
public int CategoryId { get; set; }
[Column("UpcNumber")]
public string UpcNumber { get; set; }
[Column("FigureImage")]
public string FigureImage { get; set; }
[Column("DateAdded")]
public DateTime DateAdded { get; set; }
[Column("AddedBy")]
public string AddedBy { get; set; }
}
}
CreateFigureViewModel.cs
namespace CW.Core.ViewModel
{
public class CreateFigureViewModel
{
public string FigureName { get; set; }
public string ReleaseYear { get; set; }
public int CategoryId { get; set; }
public SelectList FunkoCategories { get; set; }
public string UpcNumber { get; set; }
public string FigureImage { get; set; }
public DateTime DateAdded { get; set; }
public string AddedBy { get; set; }
}
}
Error adding data to custom table from a custom form
Hi,
I'm new to Umbraco and really want to use it for a future project however I've hit a stumbling block which seems like a basic issue but I can't seem to get over.
I have a form within a partial view which is loaded within my page with a ViewModel which contains a select list and all the other relevant fields. When the form is loaded, I populate the select list from a custom table I've got which is a categories table. When I enter all the information, I suave it down and I then get a SqlException with the following error...
"Cannot insert the value NULL into column 'CategoryId', table 'UmbracoDb.dbo.cwFigure'; column does not allow nulls. INSERT fails."
When I debug the solution I follow the post to the controller and I have a CategoryId for that property in my model so it isn't null. I can't understand why it is falling over at this. Am I missing something basic?
Below are my models and the Save action from my controller.
FigureCategory.cs
Figure.cs
CreateFigureViewModel.cs
FigureSurfaceController.cs
FigureBll.cs
Hi,
I see that your figure class doesn't have a ColumName attribute on the categoryId property.
Can you try to add that and see if it fixes it ?
Dave
Hi Dave,
Good spot on that, I missed that when I added it in. I originally structured the table without the foreign key and then added it in.
Knew it would be a basic mistake. This will forever serve as a reminder for me.
Thanks again!
is working on a reply...