[RESOLVED]MVC5- Problem when create item that lookup value from an other table

Im starting with MVC5 today. In my project, i have 2 models named: Category and Product. Each product belong to one Category.

When i select Category (ex. Computer with ID=1) and click Create button, new Product added to database but, value of Category field didn’t set to Computer. 

I check database, value of field Category_Id set to 2 (new value) and table Category has new row with ID=2, Category= null

What wrong for me?

Thanks 

Show you save action. and model graph

Thanks for your reply.

This is my code: 

//// Category model: 

public class Category
{
public int ID { get; set; }
public string CategoryName { get; set; }

}

//// Product Model:

public class Product
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime? ReleaseDate { get; set; }
public Category Category { get; set; }

}

Countinue: 

//// Product Controller: 

public ActionResult Create()
{
ViewBag.Category = new SelectList(db.Categories, "ID", "CategoryName");
return View();
}

// POST: /Product/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for

// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Category")] Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}

ViewBag.Category = new SelectList(db.Categories, "ID", "CategoryName");
return View(product);
}

Countinue: 

//// Product View- Create:

<div class="form-group">
@Html.LabelFor(model => model.Category.CategoryName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Category.ID, (SelectList)ViewBag.Category, string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
</div>

Trinh Ba Tu

public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Category")] Product product)
{

What are u getting in Product.Category.ID at this moment? debug and check

Now, i have only one category (ID=1, CategoryName="Computer").

In the Create view, I select Category = "Computer" in dropdown then click Create button. 

Break point at: if (ModelState.IsValid), VS show me value: Product.Category.ID=1, Product.Category.CategoryName=null (1)

Press F10 to move break point to  return RedirectToAction("Index");, value change to: Product.Category.ID=2, Product.Category.CategoryName=null (2)

You can see: 

(1): The value i selected is "Computer", but VS show me value is null

(2): The value of Product.Category.ID was change from 1 to 2 (new value)

Can you show me: why this happened and how to fix it ?

Thank you!

It looks like the code that you provided that represents your view is incomplete.  How are you actually submitting the form back to the server?  There is no button included on your form or any actions specified.

Oh, Amriger. 

The forum not allow to post long text. 

My view is very simple. Nothing change from default, except:

<div class="form-group">
@Html.LabelFor(model => model.Category.CategoryName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Category.ID, (SelectList)ViewBag.Category, string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
</div>

Trinh Ba Tu

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Category")] Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}

change to below and check

[HttpPost]
[ValidateAntiForgeryToken]
 public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Category")] Product product)
 {
 if (ModelState.IsValid)
 {
  var catObj=db.Categories.firstOrDefailt(c=>c.ID==product.CateGory.ID)
  product.Category=catObj;
  db.Products.Add(product);
  db.SaveChanges();
  return RedirectToAction("Index");
 }
}

Greate,

It works with small change.

Thank you very much!

Leave a Reply