views:

27

answers:

1

Can anyone help me for the following: I am having the error message. How can I correct that?

Error 4 Use of unassigned local variable 'url' on the url.Action...

UrlHelper url;
 string fullUrl = url.Action( "Details", "test", new {test.ID } );

Thanks

A: 

If you are inside the view you could directly use the helper:

<%= Url.Action( "Details", "test", new {test.ID } ) %>

If you are inside a controller action you could use the Url property:

public ActionResult Index()
{
    string fullUrl = Url.Action( "Details", "test", new {test.ID } );
    return View();
}

If this is in a helper method you already have the url:

public static void SomeHelper(this UrlHelper url)
{
    string fullUrl = url.Action( "Details", "test", new {test.ID } );
}

If it is none of the above you are probably doing something wrong and you might need to move this code.

If you are new to ASP.NET MVC I would recommend you reading some tutorials.

Darin Dimitrov
I have written the above code in my mvc model as I want to have the url in my model. What`s wrong with my existing code, how can I correct that?
The model shouldn't worry about urls. This is the responsibility of the view.
Darin Dimitrov
I need to generate the url to send via mail, and doing that from the model
It's the controller which is manipulating the model. So generate the url in the controller and pass it to the model. Notice though that `Url.Action` generates relative instead of absolute urls which is unlikely something you are wiling to send via email.
Darin Dimitrov
ok, what do you propose to me in order to generate the url and send via email? I prefer to do that in the model.
Hoe van I use the UrlHelper methods? Any example please
@user281180, You need to construct it first (as with any object). But you shouldn't do that in your model. I usually use views for anything I need to send as email and then render that view to a string and send it.
Mattias Jakobsson
I actually construct the mail by reading the database data. In my case I don`t want to use the views to send the mail. Can you give me an idea how can I use the UrlHelper. I have seen http://msdn.microsoft.com/en-us/library/dd492942(v=VS.100).aspx, but can`t see any example. Can you give me an example how to use the most appropriate method.
@user281180, I don't mean using the view to send the mail, only to generate the mail you want to send. Perfect for when you need data from the db to render the mail (as in your case). There is no example on how to use the urlhelper class from you model because you should never do that.
Mattias Jakobsson