views:

31

answers:

2

Hi Im stuck on how to do this the best way. In my case I got a Product and it got a Manufacturer Object.

So what I do is I pass the product to the view to edit. But when I do the save I look at the product object and Manufacturer is now null. I tried to do a hiddenfor for the Manufacturer Object like I do with id for the product, but that wont work. How is the best way to do this?

hope you get what I mean?

public ActionResult EditProduct(int id)
    {
        var product = _productService.GetById(id);
        return View(product);
    }

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditProduct(Product product)
    {
        //_productService.SaveOrUpdate(product);
        TempData[Message] = product.ProductName + " have been saved";

        return RedirectToAction("Products");
    }

EDIT

Product Object

public virtual int Id { get; set; }
public virtual string ProductName { get; set; }   
public virtual Manufacturer Manufacturer { get; set; }
+1  A: 

From what you say, I think the problem is the context in which you try to save your Product. When you declare it as

public ActionResult EditProduct(Product product)

it doesn't have the same "meaning" as

var product = _productService.GetById(id);

Because it lacks your database context. So I suggest you to do the following:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditProduct(int id)
{       
    var product = _productService.GetById(id);
    UpdateModel(product);
    _productService.SaveOrUpdate(product);
    TempData[Message] = product.ProductName + " have been saved";

    return RedirectToAction("Products");
}
Francisco
That dont work for me. I get exception on the UpdateModel. innerexception is null. but basicly what the UpdateModel is that is changes the input parameters that differs from the one it the database? how it gets the parameters i changed?
Dejan.S
Doh nevermind ill try this agin later. I forgot I'm using a fakeproduct repository at the moment so it really dont get the object from the database, btw does that really matter? hmm
Dejan.S
It shouldn't matter I suppose but the idea is that if your Manufacturer already exists (before Product, because of foreign key), you must fetch it from your database and "attach" it to your product, before you save it. It doesn't make sense to "pass" a manufacturer from your view, if you're updating a product. Maybe you could pass a manufacturer id as a hidden field.
Francisco
+1  A: 

try this in your View

put a hidden field for the Manufacturer.Id if this is your Manufacturer's primary key

<%=Html.HiddenFor(m=>m.Manufacturer.Id, new { @value=Model.Manufacturer.Id }) %>

this will give you a value for your Manufacturer Object in your Product. Now you just have to rebind your Manufacturer before you save it since you have your Manufacturer's primary key.

rob waminal
the fact that you are not getting any value for your Manufacturer object is that when you are submitting a form, the form is a string based key-value object, and your Manufacturer is an Entity inside an Object. Therefore an entity cannot be converted into a string. So you have to assign something in that Entity
rob waminal
For some reason the UpdateModel works now. I will keep this in mind tho. Thanks for the tip Rob
Dejan.S