ASP.NET MVC Create from a master part

I show a list of elements for this order. When the user clicks the Add Element button, I am redirected to the Element / Create page. This page collects the necessary input, but also needs to know the identifier of the order to which the item belongs. What is a suitable way to pass OrderID to Item / Create so that it saves the form message after saving the newly created item.

I played with TempData and wrote down the id on the details page via Html.Encode (). This gives me part of the path to where the identifier is displayed in the form of the element, but the value is lost when the form is submitted and submitted. I suppose because it is not part of the form. I assume my workaround is not the best way and would like to know if anyone can specify the correct way to do this in asp.net mvc.

+5
source share
3 answers

I do this by creating a new route for the Item controller, which includes OrderId. It makes no sense to have an element without an order, so OrderId is required using the restrictions parameter.

routes.MapRoute(
    "OrderItems",
    "Item/{action}/{orderId}/{id}",
    new { controller = "Item" },
    new { orderId = @"d+" }
);

, url http://<sitename>/Item/Create/8, 8 - OrderId, . http://<sitename>/Item/Delete/8/5, 8 - OrderId, 5 - ItemId.

:

public ActionResult Create(int orderId)

public ActionResult Delete(int orderId, int id)

, URL- http://<sitename>/Order/8/Item/Create http://<sitename>/Order/8/Item/Delete/5, , .

:

routes.MapRoute(
    "OrderItems",
    "Order/{orderId}/Item/{action}/{id}",
    new { controller = "Item" },
    new { orderId = @"d+" }
);
+3

(, , ):

1) Order.Details(, Order):

...
<%= Html.ActionLink("Create New Item", "Create", "OrderItem", new { orderId = Model.ID }, null)%>
...

2) OrderItem.Create:

public ActionResult Create(int orderId)
{
    ViewData["orderId"] = orderId;
    return View();
}

3) OrderItem.Create:

...
<% using (Html.BeginForm(new { orderId = ViewData["orderId"] }))
...

4) OrderItem.Create POST:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int orderId)
{
    // omitted code to create item, associated with orderId

    return RedirectToAction("Details", "Order", new { orderId = orderId });
}

- , , , , , - , .

0

To round a field that is not part of regular data entry, I usually use a hidden field in the view, for example:

<%= Html.Hidden("OrderID", Model.OrderID) %>

It looks like a form field, acts like a form field, but the user does not see it. Make sure you enter the correct OrderID code in your model from the controller.

0
source

All Articles