参数字典包含参数Orderid的空条目

时间:2017-01-19 12:49:33

标签: c# asp.net-mvc

当调用CheckoutController的Data动作时,我不断收到以下错误:

enter link description here 我正在使用这种技术在其他几个控制器中传递参数,我没有遇到任何麻烦。我的CheckoutController代码和默认路由如下所示。提前谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Functions;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class CheckoutController : Controller
    {
        private thelearningbayEntities _db = new thelearningbayEntities();
        private Auth Permission = new Auth();

        [HttpGet]
        public ActionResult Data(int Orderid) 
        {
            if (Permission.Check(0))
            {
                var email = Session["email"].ToString();
                _db.Configuration.ProxyCreationEnabled = false; 

                var result = (from order_line in _db.order_line
                              join orders in _db.orders on order_line.id_order equals orders.id_order
                              join product in _db.product on order_line.p_id equals product.p_id
                              where (orders.email == email) && (orders.id_order == Orderid)

                              select new { order_line.amount, product.p_name, product.price, product.t_image}).ToList();

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            Session["referrer"] = "/Checkout/";
            return RedirectToAction("Index", "Login");

        }
    }
}

Routeconfig:

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

namespace WebApplication2
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Product",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "MainPage", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}  

1 个答案:

答案 0 :(得分:6)

将您的操作方法参数更改为Id,以便您的请求网址与默认路由模式匹配,即{controller}/{action}/{id}

public ActionResult Data(int id) 
{
      //use id
}

修复生成此操作方法链接的代码,以明确使用orderId routeValue(查询字符串键)。

例如,如果您使用的是Html.ActionLink方法,

@Html.ActionLink("Checkout","Data","Checkout",new { orderId=20 },null)

或标记(帮助程序最终生成如下所示的标记)

<a href="/Checkout/Data?orderId=20">Checkout</a>