[英]MVC4 ActionLink not passing parameters
first MVC4 project and I've hit the buffers... 第一个MVC4项目,我已经达到了缓冲区......
I want to be able to reference my site pages thus; 我希望能够引用我的网站页面;
MENU/SUBMENU/DOCUMENT/ID 菜单/子菜单/ DOCUMENT / ID
where the ID value is optional and anything else tagged on the end, the Model will process the Document part which will be as text, eg, 如果ID值是可选的,并且最后标记了任何其他内容,则Model将处理Document部分,该部分将作为文本,例如,
HOME/WELCOME/Welcome-to-our-store HOME /首页/欢迎到我们的店
So I have a route set up (there's only 1 route) 所以我设置了一条路线(只有一条路线)
routes.MapRoute(
name: "Default",
url: "{menu}/{submenu}/{document}/{id}",
defaults: new {
controller = "Page",
action= "Data",
menu = "",
submenu = "",
document = "",
id = ""}
I have a controller; 我有一个控制器;
public class PageController : Controller
{
// Default view option for documents
public ActionResult Data(string menu, string submenu, string document, string parameters)
{
// Set the View to use rather than it default;
string thisView = "Data";
return View(thisView);
}
}
We have a _Layout.cshtml file that references a _MenuPartial.cshtml file 我们有一个引用_MenuPartial.cshtml文件的_Layout.cshtml文件
@model MyNameSpace.Models.PageModel
<ul>
@foreach (var item in Model.Menus.MainMenu)
{
<li>@Html.ActionLink(item.Label, "Page", "Data", new
{
menu = "MENU",
submenu = "SUBMENU"
},
null
)
</li>
}
</ul>
The above all writes out my main menu dynamically with the correct text etc., etc. 以上都使用正确的文本等动态地写出我的主菜单等。
However, when I click on a label, the route seems to map through happily to the PageControllers Data function but the values of string menu, string submenu, string document, string parameters are "" or null. 但是,当我单击一个标签时,路径似乎很愉快地映射到PageControllers数据函数,但字符串菜单,字符串子菜单,字符串文档,字符串参数的值是“”或null。
If I modify the URL then they get passed in correctly. 如果我修改了URL,那么它们会被正确传递。
I'm sure that this is just a misunderstanding of what I should be doing and I hope once some kind soul has spotted it I'll be off and running ! 我确信这只是对我应该做的事情的一种误解,我希望一旦某种善良的灵魂发现它我将会离开并运行!
thanks Andy 谢谢安迪
Try giving the document and id defaults the value UrlParameter.Optional
in the route and give the document and id parameters in the action the default value "" ( string document = ""
). 尝试给出文档和id默认路由中的值
UrlParameter.Optional
,并在操作中为文档和id参数提供默认值“”( string document = ""
)。 That should allow routing to reconcile the action link route data with the route. 这应该允许路由协调动作链路路由数据与路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.