如何将参数从视图输入传递到asp mvc方法?

时间:2012-05-18 20:33:33

标签: asp.net asp.net-mvc-3

我有这样的代码用于发送订购我的商品。我在浏览器中的链接看起来像这样: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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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