[RESOLVED]Passing ParentId to view

There’s two tables.

Customer and Orders.

When in Editing Customer form, there’s new Order button.

By clicking that button, I need to send the CustomerId to the New Order form.

When user presses Save, I need to be able to pass CustomerId back to controller.

How does the mechanism work?

Put the CustomerId in a hidden field.  There is an Html.HiddenFor helper that you might be able to use.

Please take a look at this post

Getting
Data From View to
Controller in
MVC

 

I can put CustomerId in a hidden field, but how do you pass it to the View from Customer form?

The default controller is like this.

public ActionResult Create(){

Return View();

}

I’ve got something like this at the moment. But how do I return it to the view?

ActionResult Create ( int? id)

{

return View();

}

like this

        ActionResult Create(int? id)
        {
            Order ordObj = new Order();
            ordObj.CustomerID = id;

            return View(ordObj);

        }

New Order View

@Html.HiddenFor(m => m.CustomerID)

or

        ActionResult Create(int? id)
        {
            ViewBag.CustomerID = id;
            return View();

        }

New Order View

@Html.Hidden("CustomerID", ViewBag.CustomerID as string)

Leave a Reply