[英]Getting an Actionresult Parameter in MVC4
I'm working on a simple MVC application, where I am generating below path 我正在开发一个简单的MVC应用程序,在其中生成以下路径
@Html.ActionLink(@objcity.CityName, "AgentProfiles", "Home", new {@Id=objcity.MaProvision.ProvinceName+"/"+@objcity.CityName }, null)
It makes a url like this: 它生成如下网址:
http://localhost:45896/Home/AgentProfiles/Ontario/testt
In the controller I have written this method: 在控制器中,我编写了此方法:
public ActionResult AgentProfiles(String Id)
{
//Code
}
Is it possible to get in /Ontario/testt in Id variable? 是否可以在ID变量中进入/ Ontario / testt?
You want to get /Ontario/testt
in Id(route parameter) for this you have to modify your default routes little bit or you have to make a custom route but in my opinion for your simple requirement try below answer. 您想为此获得Id(路由参数)中的
/Ontario/testt
,您必须稍微修改默认路由,或者必须创建自定义路由,但我认为您的简单要求请尝试以下答案。
Instead of 代替
@Html.ActionLink(@objcity.CityName, "AgentProfiles", "Home", new {@Id=objcity.MaProvision.ProvinceName+"/"+@objcity.CityName }, null)
Modify Actionlink this way 以此方式修改Actionlink
@Html.ActionLink(@objcity.CityName, "AgentProfiles", "Home", new { ProvinceName=objcity.MaProvision.ProvinceName ,CityName = objcity.CityName }, null)
Controller Action : 控制器动作:
public ActionResult AgentProfiles(string ProvinceName,string CityName ) //get ProvinceName and CityName which will be coming as querystring variables as shown here.
{......}
OR 要么
EDIT :- Try this as per your comment. 编辑:-根据您的评论尝试。
Add one more route in RouteConfig.cs file inside AppStart folder as shown below : 在AppStart文件夹内的RouteConfig.cs文件中添加另一条路由,如下所示:
routes.MapRoute(
"MvcRoutes", // Route name
"{controller}/{action}/{provincename}/{cityname}", // URL with parameters
new { controller = "Home", action = "Index", provincename = "", cityname= "" } // Parameter defaults
);
Don't forget to put this custom route above default route. 不要忘记将此自定义路由放在默认路由之上。
Modify ActionLink
as shown below : 如下所示修改
ActionLink
:
@Html.ActionLink(@objcity.CityName, "AgentProfiles", "Home", new { provincename = objcity.MaProvision.ProvinceName , cityname = objcity.CityName }, null)
Controller Action : 控制器动作:
public ActionResult AgentProfiles(string provincename ,string cityname)
{......}
you can modify your routing like- 您可以像以下修改路由
{controller}/{action}/{*catchall}
and in action method 和行动方法
public ActionResult AgentProfiles(string catchall)
{
// your code
}
Then you will have value /Ontario/testt in your catchall parameter which you can use in action method. 然后,您的catchall参数中将具有值/ Ontario / testt,您可以在操作方法中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.