0

我有这样的代码用于发送订购我的商品。我在浏览器中的链接看起来像这样:http://localhost:22764/Admin/AddToOrder?OrderId=1&WareId=1&WareCount=456但是当我尝试去那里时,我得到“/中的服务器错误”,404找不到资源。我的观点是这样的:

<% foreach (var item in Model) { %>

        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.WareId }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.WareId })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.WareId })%> ||||||
                <%: Html.ActionLink("Click Me", "AddToOrder", new { OrderId = 1, WareId = item.WareId, WareCount = item.Quantity })%>

            </td>

            <td>
                <%: item.WareName %>
            </td>
            <td>
                <%: String.Format("{0:F}", item.WareCost) %>
            </td>
            <td>
                <%: item.Quantity %>
            </td>
        </tr>

    <% } %>

和控制器方法:

 [HttpGet]

        public ActionResult AddToOrder(int OrderId, int WareId, string WareCount)
        {
            OrderRecord or = new OrderRecord();
            or.OrderId = OrderId;
            or.WareId = WareId;
            or.WareCount = WareCount;
            try
            {
                if (ModelState.IsValid)
                {
                    db.AddToOrderRecords(or);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);
            }
            return View(or);
        }

我觉得是路由麻烦?请帮帮我。

路线是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Proekt
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }
    }
}
4

1 回答 1

0

使用路由调试器查看默认路由是否可以捕获 url。通过 nugget 安装路由调试器,您可以尝试使用 ViewBag 来传递参数。

于 2012-05-18T21:15:58.793 回答