14578446
14578446

Reputation: 1074

pass values from a view to action in another controller

I am using @html.Actionlink() to build a link which redirects to an action in another controller(not the original controller with view) Below is my statement.

@Html.ActionLink(@orderNumber.ToString().Trim(), "SearchResult", "OrderStatus", @orderNumber.ToString(), new { target = "_self" })

but on clicking on the view i get an exception:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies)     could have been removed, had its name changed, or is temporarily     unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /OrderStatus/SearchResult

though i have a controller with name OrderStatus having an action with name SearchResult

Am i missing some syntax?

searchresult method:

[AcceptVerbs("POST")]
public ActionResult SearchResult(FormCollection collection)
    {
        try
        {
            string orderNumber = collection["searchItem"].ToString();

            if (string.IsNullOrEmpty(orderNumber))
                return View("OrderStatusSearch");

            ////obtain sales oder header for paased order number
            #region sales order header
            orderNumber = orderNumber.Trim();

            ObjectParameter[] parameters = new ObjectParameter[1];
            parameters[0] = new ObjectParameter("OrderNumber", orderNumber);

            var headerQuery = dbContext.ExecuteFunction<Models.SalesOrderHeader>("uspGetHeaderDetails", parameters);

            Models.SalesOrderHeader salesOrderHeader = new Models.SalesOrderHeader();
            salesOrderHeader = headerQuery.SingleOrDefault();

            ////// return empty view if sales ordr header is null
            if (salesOrderHeader == null)
                return View("EmptySearch");
            #endregion
             ....................

            ////create sales order to be passed to view
            #region Create Sales Order
            {
                Models.SalesOrder salesOrder = new Models.SalesOrder();

                salesOrder.salesOrderHeader = salesOrderHeader;
                 ..................
                return View(salesOrder);
            }
            #endregion
        }
        catch (Exception e)
        {
            return View("EmptySearch");
        }
    }

Upvotes: 1

Views: 2752

Answers (2)

Kurt S
Kurt S

Reputation: 21357

It looks like there may be one or two things wrong.

1) Are you sure are using the correct Html.ActionLink overload?

You are using the overload that is expecting Html.ActionLink([link text], [action], [controller], [route values], [htmlAttributes]) and all you've given for the route values is a string. This needs to be a RouteValueDictionary or an anonymous object, not just a string. Try this below, does this work as you expect?

@Html.ActionLink(@orderNumber.ToString().Trim(), "SearchResult", "OrderStatus", new{ id = @orderNumber.ToString() }, new { target = "_self" })

I assume you want it to generate a URL such as /OrderStatus/SearchResult/999 where 999 is whatever order number you have. Is this right?

2) Your SearchResult ActionResult only accepts a POST. Clicking on a hyperlink generated by the ActionLink will result in a GET. Since you don't have a Controller/Action by these names that accepts a GET, a 404 is the expected result.

If the link your ActionLink generates points to /OrderStatus/SearchResult[/order number] then an appropriate action method would have to look like (assuming default routing configuration):

public ActionResult SearchResult(string orderNumber) { ... }

You could prefix it with the attribute [HttpGet] if you wanted this action to only allow GETs, but this is not required.

Upvotes: 3

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

You link code is wrong , try following

@Html.ActionLink("NameToBeDisplayed", "SearchResult", "OrderStatus", new { yourParameter = @OrderNumber.ToString() })

Upvotes: 0

Related Questions