Reputation: 73928
When posting the html form I can see null in the controller, so I am not able to add a Product to the database. What is wrong here?
public partial class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
}
public ActionResult CreateProduct()
{
return View();
}
[HttpPost]
public ActionResult CreateProduct(Product product) // this is always null
{
service.CreateProduct(product);
return RedirectToAction("Index");
}
@model WebApplication1.Models.Product
@{
ViewBag.Title = "CreateProduct";
}
<h2>CreateProduct</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
<input type="submit" value="Create" />
</fieldset>
}
Upvotes: 1
Views: 97
Reputation: 254
in your get pass the model with empty values
public ActionResult CreateProduct()
{
Product prd= new Product();
return View(prd);
}
and check if this solves your problem
Upvotes: 4