Home > Software design >  How do I call another controller from my view
How do I call another controller from my view

Time:01-20

Hey guys so my problem is I keep getting the Error Page not found 404 when I look in dbo.Log table (My sql database)

Im trying to render a view via a button push from another view, the first view works fine this is my button from my first view

<div >
    <input type="button"
           value="Go to Edit"
           onclick="location.href='@Url.Action("Edit", "CheckoutAttributes", 200)'" />

</div>
</body>
</html>

I know the 200 is wrong, I was trying around to call the controller, but even without the 200 it doesnt work

This is my 2 actions in controller thats supposed to render the new view:

    //[AuthorizeAdmin]
    //[Area(AreaNames.Admin)]
    //trying something, adding areas (end) didnt work
    [HttpGet("/[area]/[controller]/[action]/{orderId:int}")]
    public IActionResult Edit(int orderId)
    {
        Order? order = _orderService.GetOrderById(orderId);

        if (order == null)
        {
            return NotFound(orderId);
        }

        if (order.OrderStatus != OrderStatus.Complete)
        {
            return CannotEdit(orderId, $"{nameof(order.OrderStatus)} is '{order.OrderStatus}', but '{OrderStatus.Complete}' is required.");
        }

        if (order.PaymentStatus != PaymentStatus.Paid)
        {
            return CannotEdit(orderId, $"{nameof(order.PaymentStatus)} is '{order.PaymentStatus}', but '{PaymentStatus.Paid}' is required.");
        }

        if (order.ShippingStatus != ShippingStatus.Delivered)
        {
            return CannotEdit(orderId, $"{nameof(order.ShippingStatus)} is '{order.ShippingStatus}', but '{ShippingStatus.Delivered}' is required.");
        }

        return Edit(order);
    }

    //[AuthorizeAdmin]
    //[Area(AreaNames.Admin)]
    //trying something, adding areas (end) didnt work
    private IActionResult Edit(Order order)
    {
        var model = new CheckoutAttributesEditModel
        {
            OrderId = order.Id,
        };

        ParseXml(order, model);
        ParseDescription(order, model);

        if (!model.AllPredefinedPurchaseReasons.Any())
        {
            // Should not occur under normal circumstances (only when neither the xml nor the description could be parsed).

            (CheckoutAttribute publishAttribute, CheckoutAttribute predefinedPurchaseReasonAttribute, CheckoutAttribute customPurchaseReasonAttribute) = GetCheckoutAttributes();

            model.AllPredefinedPurchaseReasons = predefinedPurchaseReasonAttribute.CheckoutAttributeValues.ToList();

            model.PublishLabel = GetLocalisedAttributeName(publishAttribute);
            model.PredefinedPurchaseReasonLabel = GetLocalisedAttributeName(predefinedPurchaseReasonAttribute);
            model.CustomPurchaseReasonLabel = GetLocalisedAttributeName(customPurchaseReasonAttribute);
        }

        ParseVCHistory(order.Id, model);

        if (model.SelectedPredefinedPurchaseReason <= 0)
        {
            // Should be very unlikely to happen.
            model.SelectedPredefinedPurchaseReason = model.AllPredefinedPurchaseReasons.First().Id;
        }
        //trying somehing, original was return View(model);
        return View($"{Plugin.RelativeDirectoryPath}/Views/CheckoutAttributes/Edit.cshtml", model);
    }

What am I doing wrong? Keep getting 404 page not found thanks in advance

CodePudding user response:

Add areas in endpoint

 endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");

                endpoints.MapControllerRoute(
                   name: "areas",
                   pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

And create link:

<input type="button"
       value="Go to Edit"
       onclick="location.href='@(Url.Action("Edit",new { controller = "CheckoutAttributes",id="123456" }))'" />

CodePudding user response:

or

<input type="button" value="Go to Edit" onclick="location.href='@(Url.Action("Edit","CheckoutAttributes",new { id="123456" }))'" />

  •  Tags:  
  • Related