[英]ActionResult parameter coming in as null
I have seen a couple related questions to this, but I cannot seem to solve my exact instance. 我已经看到了几个与此相关的问题,但是我似乎无法解决确切的问题。 I have a url such as
http://127.0.0.1/UTS/Unit/J0000001
. 我有一个网址,例如
http://127.0.0.1/UTS/Unit/J0000001
。 That is, my controller is UTS
, my action is Unit
and there is an optional parameter c_number
. 也就是说,我的控制器是
UTS
,我的动作是Unit
并且有一个可选参数c_number
。
I have my route configured as follows: 我的路线配置如下:
routes.MapRoute(
name: "Unit",
url: "{controller}/{action}/{c_number}",
defaults: new { controller = "UTS", action = "Unit", c_number = "" }
);
Then, my action in my controller as: 然后,我在我的控制器行动 :
public ActionResult Unit(string c_number)
{
UnitViewModel uvm = new UnitViewModel();
uvm.unit = pacificRepo.Units.FirstOrDefault(g => g.c_number == c_number);
uvm.geoLocation = pacificRepo.GeoLocations.FirstOrDefault(g => g.geo_location.Latitude == uvm.unit.geo_location.Latitude && g.geo_location.Longitude == uvm.unit.geo_location.Longitude);
return View(uvm);
}
Whenever I go to the example URL I gave above, c_number
comes up as null
. 每当我转到上面给出的示例URL时,
c_number
就会显示为null
。 It should be J0000001
. 应该是
J0000001
。 Does anyone see something blatantly obvious that I'm missing? 有人看到我失踪的明显证据吗?
Since you define it in your Route
you don't need to add parameter for your c_number
.You can get your value from RouteData
dictionary.That c_number
parameter has value only if you pass it as QueryString
like http://someurl.com/UTS/Unit?c_number="J0000001"
由于您在
Route
定义了它,因此不需要为c_number
添加参数。您可以从RouteData
字典中c_number
值。只有当您将其作为QueryString
传递给http://someurl.com/UTS/Unit?c_number="J0000001"
该c_number
参数才具有值http://someurl.com/UTS/Unit?c_number="J0000001"
public ActionResult Unit()
{
var cNumber = RouteData.Values["c_number"].ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.