0

Ok so im trying to input data from View to a SQL table i got no errorss but data does not show in the table I using VisualStudio project is ASP.NET MVC 4 Web Application so it has already created models views connectionstring controllers and it has a Site.Master so i just make new View from which i will pass data to my controller and then push it to SQL table which i have already created within "Table" folder which is created by default from the project in this "Table" folder i have tables which are needed for registration stuff already created by its own so i just added a new table to this "Table" folder called ProductTable

So ProductTable has 2 values (int)ID which is Primary Keyed and (string)Name

Ok so in "Model" folder i added new Class "ProductTable" with code inside

        [Table("ProductTable")]
        public class ProductTable
        {
            [Key]
            public int ID { get; set; }
            [Required]
            public string Name { get; set; }

        }

i have created another class there "ProductTableContext " with code inside

public class ProductTableContext : DbContext
    {
        public DbSet<ProductTable> Products { get; set; }

    }

and in my HomeController i have

public class HomeController : Controller
    {



        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your .";

            return View();
        }
        public ActionResult Table()
        {
            return View(new ProductTable());
        }
        [HttpPost]       
        public ActionResult Table(ProductTable product)
        {
            if (!ModelState.IsValid)
            {
                return View(product);
            }
            using (var contex = new ProductTableContext())
            {
                contex.Products.Add(product);
                contex.SaveChanges();
            }
            return View();

        }


    }

so Index , About and Contact are Views created by the project itself. and View

@model Father.Models.ProductTable

@{
    ViewBag.Title = "Table";
}

<h2>Table</h2>
@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </div>



    <button type="submit">Create event</button>
}

So the problem is that everything works fine i have no errors but when i close the website and i press debug > stop debugging then i click ShowTableData on ProductTable in ServerExplorer all values are Nulls so its like ihave never added this Data to the table can anyone tell me whats wrong? Thank you!

spirit_seller
  • 31
  • 1
  • 4
  • I would set a breakpoint in your Table action and make sure that the ProductTable is actually populated before you are saving it to context. – str8killinit Mar 06 '14 at 19:28
  • i think i made it works but i got an error now "Cannot insert the value NULL into column 'ID', table 'aspnet-Father-##########.dbo.ProductTable'; column does not allow nulls. INSERT fails." – spirit_seller Mar 06 '14 at 19:49
  • Looks like your ID field is not set to have the database handle auto-generating the values. Take a look at this question: http://stackoverflow.com/questions/16079217/how-to-generate-and-auto-increment-id-with-entity-framework – str8killinit Mar 06 '14 at 20:47

1 Answers1

0

Add this attribute to your ID property [DatabaseGenerated(DatabaseGeneratedOption.Identity)]. This will tell EF to have the database generate the Identity when you save the object.

str8killinit
  • 167
  • 7