0

Hi,

I am using ASP.NET MVC 3 and I need to generate a URL like this : MyURL.se/?CS.C2=113

I have tried this :

<%= Html.ActionLink(subItem.Name, "List", "Ad", new { CS.C2=subItem.Id}, null) %>

But this will throw a : CS0746: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

CS.C2 is part of the viewClass that the action needs.

So how do I generate a proper URL for this link?

Edit :

public ActionResult List(AdList data)
{
    //Fill data
    return View(data);
}

I have tried this : https://stackoverflow.com/a/10011614/1490727 but it throws :

No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Community
  • 1
  • 1
Ivy
  • 2,285
  • 4
  • 24
  • 29

2 Answers2

1

I ended up custructing it manually like this :

<a href="?CS.C2=<%=item.Id%>" title="<%=item.Name%>"><%=item.Name%></a>

If its possible to generate this link with Action.Link I would be glad to hear about it.

Ivy
  • 2,285
  • 4
  • 24
  • 29
  • Did you try using Razor syntax to clean it up a bit? Also you can create a custom route in Global.asax that will generate the appropriate link structure you desire. – Only Bolivian Here Jul 08 '12 at 15:55
  • I have not yet migrated to Razor(its alot of work in this solution). I was trying to create a new custom route but I did not get it right so the above was a simple fix. – Ivy Jul 10 '12 at 17:52
1

I could do something like this..

a. create a custom html helper

public static MvcHtmlString MyActionLink(this HtmlHelper htmlHelper, string linkText,
string action, string controller, IDictionary<string, object> routeValues, 
object htmlAttributes)
{
         return htmlHelper.ActionLink
         (
            linkText, 
            action, 
            controller, 
            new RouteValueDictionary(routeValues), 
            HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
         );
}

b. Use it like this

@Html.MyActionLink(subItem.Name, "List", "Ad", 
new Dictionary<string, object> { { "CS.C2", subItem.Id } }, null)

UPDATE

You don't need custom action helper. You could directly pass the RouteValueDictionary as below,

@Html.ActionLink(subItem.Name, "List", "Ad", 
new RouteValueDictionary{ { "CS.C2", subItem.Id } }, null)
VJAI
  • 32,167
  • 23
  • 102
  • 164