为什么RouteData.Values [“someValue”]在Controller中显示为null?

时间:2012-10-03 14:33:42

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

我的网址是:http://localhost:2570/Test/Index/5/100100/44,其中5 = id,100100 = jobno,44 = instid。但是,当我尝试在Controller中访问这些值时,我得到的只是null。我错过了什么?

TestController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InspectionWebFormsMVC.ViewModels;
using InspectionWebFormsMVC.Models;
using InspectionWebFormsMVC.Services;
using System.Data;

namespace Stuff.Controllers
{
    public class TestController : Controller
    {
        private WebFormsDataDbContext dbModels;

        public TestController()
        {
            dbModels = new WebFormsDataDbContext();
        }

        [HttpPost]
        public ActionResult SaveEmailStatus(string empid, int? instid, long? emailid, long? ctrltypeid)
        {

            if (ModelState.IsValid)
            {
            }

            var bar = this.ControllerContext.RouteData.Values["id"];
            var blah = this.RouteData.Values["id"];
            var foo = this.RouteData.Values["instid"];
            int? iid = instid;
            int? formid = Convert.ToInt32(RouteData.Values["id"].ToString());
            return RedirectToAction("Index", new { id = formid.Value, instid = iid.Value});
        }
    }
}

Index.cshtml

@model IEnumerable<Stuff.Models.Part>

@{
    ViewBag.Title = "Testing Editor Templates";
}

@using System.Web.WebPages;
@using System.Web.Script.Serialization;
@using Stuff.ViewModels;

<h2>Test Control for Webforms</h2>


@using (Html.BeginForm("SaveEmailStatus", "Test", new { instid = ViewContext.RouteData.Values["instid"], empid = "107" }, FormMethod.Post))
{
    <div style="clear:both;">
        <div style="float:right;">
            <div style="text-align:left;">                
                @foreach (var line in ViewBag.EmailStatus as List<FormRowModel>)
                {
                    @line.Description.ToString()
                    <br /><br />
                    @line.RowInput[0].RowCtrl.DefaultValues[0].Label 
                    @: &nbsp; 
                    @line.RowInput[0].InputtedDate.ToString()
                    <br />  
                    @line.RowInput[1].RowCtrl.DefaultValues[0].Label 
                    @: &nbsp; 
                    @line.RowInput[0].InputtedData
                    <br /><br />
                    @Html.Hidden("emailid", line.RowInput[0].InputtedDataID)
                    @Html.Hidden("ctrltypeid", line.RowInput[0].RowCtrl.CtrlTypeID)
                }               
                <input type="submit" value="Send Email" />
            </div>
        </div>
    </div>
}

的Global.asax.cs

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

namespace Stuff
{
    // 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 RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

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

            routes.MapRoute(
                "ProjectInfos", // Route name
                "ProjectInfo/{action}/{id}/{jobno}/{instid}/{section}", // URL with parameters
                new { controller = "ProjectInfo", action = "Index", id = UrlParameter.Optional, jobno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "SectionsData", // Route name
                "Test/Sections/{id}/{jobno}/{instid}/{section}", // URL with parameters
                new { controller = "TestEdit", action = "Sections", id = UrlParameter.Optional, jobno = UrlParameter.Optional, instid = UrlParameter.Optional, section = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
                "Sections", // Route name
                "Test/Index/{id}/{jobno}/{instid}", // URL with parameters
                new { controller = "TestEdit", action = "Index", id = UrlParameter.Optional, jobno = UrlParameter.Optional, instid = UrlParameter.Optional } // Parameter defaults
            );

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

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

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

        }

1 个答案:

答案 0 :(得分:1)

从您发布的代码中,输出

@using (Html.BeginForm("SaveEmailStatus", "Test", 
       new { instid = ViewContext.RouteData.Values["instid"], empid = "107" },
       FormMethod.Post))

将是

<form method="post" action="/Test/SaveEmailStatus?instid=44&empid=107"> 

因此,假设您发布的路线是正确的,除了最后一个(默认)路线之外,它们都不会匹配该路线。该路线在RouteData中只有3个键,controlleractionid

由于您发布的是表单,因此您的网址中可能不需要instidempid,而只需将其作为隐藏的输入与您的表单一起发布。