Question

Possible Duplicate:
HTML.ActionLink method

In my ASP.NET MVC 3 application, I wish to generate an action link from within a model class, corresponding to calling Html.ActionLink in a view. The reason is I need to return links via JSON to DataTables, to have them rendered in table cells. How should I do this?

Specifically, the model class contains a property that returns a string containing links into my application. DataTables receives JSON representations of instances of the model class, and uses this particular property to fill in table cells.

Était-ce utile?

La solution

I would avoid having the model generate its own links, but have the controller create the links and set the property with the resulting values. Anyways, you can create and consume UrlHelper in a controller like this:

var url = new UrlHelper(ControllerContext.RequestContext);
url.Action(...);

If you don't have access to controller context (like you decide to do this within your model separately from the view or controller), you can use

UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top