Reputation: 5131
My URL is this: http://localhost:2570/Test/Index/5/100100/44
, where 5 = id, 100100 = jobno, and 44 = instid. However, when I try to access these values in the Controller all I get is null. What am I missing?
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
@:
@line.RowInput[0].InputtedDate.ToString()
<br />
@line.RowInput[1].RowCtrl.DefaultValues[0].Label
@:
@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
);
}
Upvotes: 0
Views: 5307
Reputation: 6832
From the code you have posted, the output of
@using (Html.BeginForm("SaveEmailStatus", "Test",
new { instid = ViewContext.RouteData.Values["instid"], empid = "107" },
FormMethod.Post))
would be
<form method="post" action="/Test/SaveEmailStatus?instid=44&empid=107">
So assuming the routes you have posted are correct, none of them will match that URL except the last (default) one. That route will have only 3 keys in RouteData, controller
, action
and id
.
Since you're posting a form, you probably don't need instid
and empid
in your url, instead just post them as hidden inputs along with your form.
Upvotes: 1