Passing multiple value from MVC3 View to an ActionMethod

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I wonder if anybody can help me here. I apologise for sounding like a thicko but I'm new to MVC3 and I'm trying to pass 2 values from a view to an action method but it just isn't playing fair!

HTML:

@Html.ActionLink("ASSIGN", "AssignTokenToDataTemplate", "HostHtmlTokenManager",  new { htmlTokenId = item.Id }, new { htmlDataTemplateId = 1 }) 

ACTION METHOD:

public ActionResult AssignTokenToDataTemplate(int htmlTokenId, int htmlDataTemplateId) {     // Do some database stuff here     return View("AssignAnExistingTokenToHtmlDataTemplate", new {templateId = htmlDataTemplateId}); } 

I want to pass two integers into the AssignTokenToDataTemplate action method but I cannot get it to work?!

Can anybody see where I'm going wrong? :(

回答1:

You could pass both values using the routeValues parameter:

@Html.ActionLink(     "ASSIGN",                             // linkText     "AssignTokenToDataTemplate",          // actionName     "HostHtmlTokenManager",               // controllerName     new {                                 // routeValues         htmlTokenId = item.Id,          htmlDataTemplateId = 1      },      null                                  // htmlAttributes ) 


回答2:

Try

@Html.ActionLink("ASSIGN", "AssignTokenToDataTemplate", "HostHtmlTokenManager",  new { htmlTokenId = item.Id , htmlDataTemplateId = 1 }) 

However you might want to consider using a model (a type of your own) to pass them together as one.

Kindness,

D



回答3:

You have to include both parameters in the anonymous class:

@Html.ActionLink("ASSIGN", "AssignTokenToDataTemplate", "HostHtmlTokenManager",              null, new { htmlDataTemplateId = 1, htmlTokenId = item.Id }) 


回答4:

Try;

@Html.ActionLink("ASSIGN", "AssignTokenToDataTemplate", "HostHtmlTokenManager",  new { htmlTokenId = item.Id, htmlDataTemplateId = 1 }) 

Matt



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!