How can I return a string with @Url.Action












-1















How can I return a model property with url and put it on onclick like that:



Model:



public class MyModel
{
public string link {get;set;}
}


Controller:



public ActionResult Index()
{
MyModel oMyModel = new MyModel();
oMyModel.Link = "@Url.Action('" + "Index" + "', '" + "Users" + "')";

return View(oMyModel);
}


In view:



 <a class="font" href="#" onclick="@Model.LINK">Click Here</a>


But in html it does not render corrently










share|improve this question




















  • 2





    model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

    – akd
    Nov 22 '18 at 17:34













  • Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

    – Pete
    Nov 22 '18 at 17:38













  • Also, use the href attribute of the anchor tag!

    – Shyju
    Nov 22 '18 at 17:40











  • It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

    – Vasco Silva
    Nov 22 '18 at 17:41








  • 1





    it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

    – Pete
    Nov 22 '18 at 17:48


















-1















How can I return a model property with url and put it on onclick like that:



Model:



public class MyModel
{
public string link {get;set;}
}


Controller:



public ActionResult Index()
{
MyModel oMyModel = new MyModel();
oMyModel.Link = "@Url.Action('" + "Index" + "', '" + "Users" + "')";

return View(oMyModel);
}


In view:



 <a class="font" href="#" onclick="@Model.LINK">Click Here</a>


But in html it does not render corrently










share|improve this question




















  • 2





    model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

    – akd
    Nov 22 '18 at 17:34













  • Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

    – Pete
    Nov 22 '18 at 17:38













  • Also, use the href attribute of the anchor tag!

    – Shyju
    Nov 22 '18 at 17:40











  • It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

    – Vasco Silva
    Nov 22 '18 at 17:41








  • 1





    it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

    – Pete
    Nov 22 '18 at 17:48
















-1












-1








-1








How can I return a model property with url and put it on onclick like that:



Model:



public class MyModel
{
public string link {get;set;}
}


Controller:



public ActionResult Index()
{
MyModel oMyModel = new MyModel();
oMyModel.Link = "@Url.Action('" + "Index" + "', '" + "Users" + "')";

return View(oMyModel);
}


In view:



 <a class="font" href="#" onclick="@Model.LINK">Click Here</a>


But in html it does not render corrently










share|improve this question
















How can I return a model property with url and put it on onclick like that:



Model:



public class MyModel
{
public string link {get;set;}
}


Controller:



public ActionResult Index()
{
MyModel oMyModel = new MyModel();
oMyModel.Link = "@Url.Action('" + "Index" + "', '" + "Users" + "')";

return View(oMyModel);
}


In view:



 <a class="font" href="#" onclick="@Model.LINK">Click Here</a>


But in html it does not render corrently







c# html asp.net-mvc razor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:04







user3559349

















asked Nov 22 '18 at 17:32









Vasco SilvaVasco Silva

66




66








  • 2





    model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

    – akd
    Nov 22 '18 at 17:34













  • Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

    – Pete
    Nov 22 '18 at 17:38













  • Also, use the href attribute of the anchor tag!

    – Shyju
    Nov 22 '18 at 17:40











  • It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

    – Vasco Silva
    Nov 22 '18 at 17:41








  • 1





    it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

    – Pete
    Nov 22 '18 at 17:48
















  • 2





    model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

    – akd
    Nov 22 '18 at 17:34













  • Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

    – Pete
    Nov 22 '18 at 17:38













  • Also, use the href attribute of the anchor tag!

    – Shyju
    Nov 22 '18 at 17:40











  • It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

    – Vasco Silva
    Nov 22 '18 at 17:41








  • 1





    it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

    – Pete
    Nov 22 '18 at 17:48










2




2





model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

– akd
Nov 22 '18 at 17:34







model.Link = Url.Action(“actionName”,”controllerName”); this is the way to generate the url in your controller. For your case Link = Url.Action(“Index”,”Users”);

– akd
Nov 22 '18 at 17:34















Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

– Pete
Nov 22 '18 at 17:38







Also it would be @Model.Link (Case sensitive) Although you have three different cases for that link property!

– Pete
Nov 22 '18 at 17:38















Also, use the href attribute of the anchor tag!

– Shyju
Nov 22 '18 at 17:40





Also, use the href attribute of the anchor tag!

– Shyju
Nov 22 '18 at 17:40













It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

– Vasco Silva
Nov 22 '18 at 17:41







It dont render correctly:the html result is <a class="child-font" href="#" onclick="Url.Action(&quot;Index&quot;,&quot;Cliente&quot;);">Visão Global</a>

– Vasco Silva
Nov 22 '18 at 17:41






1




1





it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

– Pete
Nov 22 '18 at 17:48







it should be there - again, you need to remove the quotes and make sure you are using System.Web.Mvc;

– Pete
Nov 22 '18 at 17:48














1 Answer
1






active

oldest

votes


















0














First, decide on a case. You're using link in your model, Link in your controller, and LINK in your view. In C#, those all mean different things. Choose one and make them all the same.



Second, the @ notation is Razor syntax. You are adding it to a string and then using razor to inject that string into your view. But by that time, it's not going to get processed by Razor again, so it stays as @Url.Action....



You should either do this in your controller, so it sets the variable to the actual URL (not just "@Url.Action..."):



oMyModel.Link = Url.Action("Index", "Users");


Or preferably, just call Url.Action in your view, which will give you the exact same result:



<a class="font" href="@Url.Action("Index", "Users")">Click Here</a>





share|improve this answer


























  • But I want define url in controller and use in view. In controller, URL is not recognized.

    – Vasco Silva
    Nov 22 '18 at 17:47











  • Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

    – Gabriel Luci
    Nov 22 '18 at 17:49











  • Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

    – Gabriel Luci
    Nov 22 '18 at 17:53













  • It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

    – Vasco Silva
    Nov 22 '18 at 18:02













  • The Index action is always the default, so "/Users" will use the Index action in the Users controller.

    – Gabriel Luci
    Nov 22 '18 at 18:04













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435900%2fhow-can-i-return-a-string-with-url-action%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First, decide on a case. You're using link in your model, Link in your controller, and LINK in your view. In C#, those all mean different things. Choose one and make them all the same.



Second, the @ notation is Razor syntax. You are adding it to a string and then using razor to inject that string into your view. But by that time, it's not going to get processed by Razor again, so it stays as @Url.Action....



You should either do this in your controller, so it sets the variable to the actual URL (not just "@Url.Action..."):



oMyModel.Link = Url.Action("Index", "Users");


Or preferably, just call Url.Action in your view, which will give you the exact same result:



<a class="font" href="@Url.Action("Index", "Users")">Click Here</a>





share|improve this answer


























  • But I want define url in controller and use in view. In controller, URL is not recognized.

    – Vasco Silva
    Nov 22 '18 at 17:47











  • Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

    – Gabriel Luci
    Nov 22 '18 at 17:49











  • Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

    – Gabriel Luci
    Nov 22 '18 at 17:53













  • It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

    – Vasco Silva
    Nov 22 '18 at 18:02













  • The Index action is always the default, so "/Users" will use the Index action in the Users controller.

    – Gabriel Luci
    Nov 22 '18 at 18:04


















0














First, decide on a case. You're using link in your model, Link in your controller, and LINK in your view. In C#, those all mean different things. Choose one and make them all the same.



Second, the @ notation is Razor syntax. You are adding it to a string and then using razor to inject that string into your view. But by that time, it's not going to get processed by Razor again, so it stays as @Url.Action....



You should either do this in your controller, so it sets the variable to the actual URL (not just "@Url.Action..."):



oMyModel.Link = Url.Action("Index", "Users");


Or preferably, just call Url.Action in your view, which will give you the exact same result:



<a class="font" href="@Url.Action("Index", "Users")">Click Here</a>





share|improve this answer


























  • But I want define url in controller and use in view. In controller, URL is not recognized.

    – Vasco Silva
    Nov 22 '18 at 17:47











  • Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

    – Gabriel Luci
    Nov 22 '18 at 17:49











  • Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

    – Gabriel Luci
    Nov 22 '18 at 17:53













  • It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

    – Vasco Silva
    Nov 22 '18 at 18:02













  • The Index action is always the default, so "/Users" will use the Index action in the Users controller.

    – Gabriel Luci
    Nov 22 '18 at 18:04
















0












0








0







First, decide on a case. You're using link in your model, Link in your controller, and LINK in your view. In C#, those all mean different things. Choose one and make them all the same.



Second, the @ notation is Razor syntax. You are adding it to a string and then using razor to inject that string into your view. But by that time, it's not going to get processed by Razor again, so it stays as @Url.Action....



You should either do this in your controller, so it sets the variable to the actual URL (not just "@Url.Action..."):



oMyModel.Link = Url.Action("Index", "Users");


Or preferably, just call Url.Action in your view, which will give you the exact same result:



<a class="font" href="@Url.Action("Index", "Users")">Click Here</a>





share|improve this answer















First, decide on a case. You're using link in your model, Link in your controller, and LINK in your view. In C#, those all mean different things. Choose one and make them all the same.



Second, the @ notation is Razor syntax. You are adding it to a string and then using razor to inject that string into your view. But by that time, it's not going to get processed by Razor again, so it stays as @Url.Action....



You should either do this in your controller, so it sets the variable to the actual URL (not just "@Url.Action..."):



oMyModel.Link = Url.Action("Index", "Users");


Or preferably, just call Url.Action in your view, which will give you the exact same result:



<a class="font" href="@Url.Action("Index", "Users")">Click Here</a>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 18:17

























answered Nov 22 '18 at 17:45









Gabriel LuciGabriel Luci

11.2k11525




11.2k11525













  • But I want define url in controller and use in view. In controller, URL is not recognized.

    – Vasco Silva
    Nov 22 '18 at 17:47











  • Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

    – Gabriel Luci
    Nov 22 '18 at 17:49











  • Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

    – Gabriel Luci
    Nov 22 '18 at 17:53













  • It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

    – Vasco Silva
    Nov 22 '18 at 18:02













  • The Index action is always the default, so "/Users" will use the Index action in the Users controller.

    – Gabriel Luci
    Nov 22 '18 at 18:04





















  • But I want define url in controller and use in view. In controller, URL is not recognized.

    – Vasco Silva
    Nov 22 '18 at 17:47











  • Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

    – Gabriel Luci
    Nov 22 '18 at 17:49











  • Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

    – Gabriel Luci
    Nov 22 '18 at 17:53













  • It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

    – Vasco Silva
    Nov 22 '18 at 18:02













  • The Index action is always the default, so "/Users" will use the Index action in the Users controller.

    – Gabriel Luci
    Nov 22 '18 at 18:04



















But I want define url in controller and use in view. In controller, URL is not recognized.

– Vasco Silva
Nov 22 '18 at 17:47





But I want define url in controller and use in view. In controller, URL is not recognized.

– Vasco Silva
Nov 22 '18 at 17:47













Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

– Gabriel Luci
Nov 22 '18 at 17:49





Make sure you're using the right case. It's Url, not URL. Url.Action is recognized in a controller. I just tried it.

– Gabriel Luci
Nov 22 '18 at 17:49













Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

– Gabriel Luci
Nov 22 '18 at 17:53







Url is a property of Controller, so make sure your controller class is inheriting from Controller (although I don't know how your app could even work if it wasn't)

– Gabriel Luci
Nov 22 '18 at 17:53















It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

– Vasco Silva
Nov 22 '18 at 18:02







It only render onclick="/Users" , not onclick="/Users/Index" . Error: Invalid regular expression: missing /

– Vasco Silva
Nov 22 '18 at 18:02















The Index action is always the default, so "/Users" will use the Index action in the Users controller.

– Gabriel Luci
Nov 22 '18 at 18:04







The Index action is always the default, so "/Users" will use the Index action in the Users controller.

– Gabriel Luci
Nov 22 '18 at 18:04






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435900%2fhow-can-i-return-a-string-with-url-action%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?