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.

2
  • Could you post your action code too? Thanks. Commented Jul 8, 2012 at 12:54
  • Posted, the Adlist is a complex class and the settings I need to set is AdList.CS.CS2. Commented Jul 8, 2012 at 12:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.