How can I return a string with @Url.Action
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
|
show 3 more comments
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
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 thehref
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("Index","Cliente");">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
|
show 3 more comments
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
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
c# html asp.net-mvc razor
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 thehref
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("Index","Cliente");">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
|
show 3 more comments
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 thehref
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("Index","Cliente");">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("Index","Cliente");">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("Index","Cliente");">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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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>
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'sUrl
, notURL
.Url.Action
is recognized in a controller. I just tried it.
– Gabriel Luci
Nov 22 '18 at 17:49
Url
is a property ofController
, so make sure your controller class is inheriting fromController
(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
TheIndex
action is always the default, so "/Users" will use theIndex
action in theUsers
controller.
– Gabriel Luci
Nov 22 '18 at 18:04
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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'sUrl
, notURL
.Url.Action
is recognized in a controller. I just tried it.
– Gabriel Luci
Nov 22 '18 at 17:49
Url
is a property ofController
, so make sure your controller class is inheriting fromController
(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
TheIndex
action is always the default, so "/Users" will use theIndex
action in theUsers
controller.
– Gabriel Luci
Nov 22 '18 at 18:04
|
show 3 more comments
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>
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'sUrl
, notURL
.Url.Action
is recognized in a controller. I just tried it.
– Gabriel Luci
Nov 22 '18 at 17:49
Url
is a property ofController
, so make sure your controller class is inheriting fromController
(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
TheIndex
action is always the default, so "/Users" will use theIndex
action in theUsers
controller.
– Gabriel Luci
Nov 22 '18 at 18:04
|
show 3 more comments
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>
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>
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'sUrl
, notURL
.Url.Action
is recognized in a controller. I just tried it.
– Gabriel Luci
Nov 22 '18 at 17:49
Url
is a property ofController
, so make sure your controller class is inheriting fromController
(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
TheIndex
action is always the default, so "/Users" will use theIndex
action in theUsers
controller.
– Gabriel Luci
Nov 22 '18 at 18:04
|
show 3 more comments
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'sUrl
, notURL
.Url.Action
is recognized in a controller. I just tried it.
– Gabriel Luci
Nov 22 '18 at 17:49
Url
is a property ofController
, so make sure your controller class is inheriting fromController
(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
TheIndex
action is always the default, so "/Users" will use theIndex
action in theUsers
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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("Index","Cliente");">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