简体   繁体   English

如何从我的角度调用另一个 controller

[英]How do I call another controller from my view

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)嘿伙计们,所以我的问题是当我查看 dbo.Log 表(我的 sql 数据库)时,我不断收到错误页面未找到 404

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 class="content2">
        <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我知道 200 是错误的,我试图打电话给 controller,但即使没有 200 它也不起作用

This is my 2 actions in controller thats supposed to render the new view:这是我在 controller 中的 2 个操作,应该呈现新视图:

        //[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不断收到 404 页面未找到提前谢谢

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" }))'" />

or或者

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM