I have an action method signature of which is:
public ActionResult ViewContest(int id, int showNext){
//Some code here
}
In one of my views, I have this:
<a href="#Html.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })">
<img src="~/Image/RightArrow.png" style="float:right;width:40px;" />
</a>
When I run the program, the view passes the "id" value to the method correctly but the runtime throws an exception for showNext being null. Why is my showNext value not passing to the method? Any ideas?
You re using wrong helper. To generate url to ViewContest method of Home controller use:
#Url.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })
So eventually your code should look like:
<a href="#Url.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })">
<img src="~/Image/RightArrow.png" style="float:right;width:40px;" />
</a>
Related
I am getting confuse in understanding the query string and routing concept in asp.net mvc, and I will appreciate if some one can please help.
My problem is:
I have a login page where user will enter the Username, password and Id. After successful login I like to take the Id and pass it to my Profile page. Now profile page is part of layout page along with two other links.
<nav class="main">
<ul class="menu" >
<li>#Html.ActionLink("Profile", MVC.Profile.Index(), new { Id = Request.QueryString["Id"].ToString() })</li>
<li>#Html.ActionLink("Configuration", MVC.Configuration.Index(),new { Id = Request.QueryString["Id"].ToString() })</li>
<li>#Html.ActionLink("Transaction", MVC.Transaction.Index(), new { Id = Request.QueryString["Id"].ToString() })</li>
</ul>
</nav>
From my login page I am using the following code to Redirect the user to Profile page.
return RedirectToAction("Index", "Profile", new { Id =
viewModel.Id });
Now how can i pass the same value to configuration and transaction page, they are just action links on layout page.I thought of using Request.QueryString like below:
new { Id = Request.QueryString["Id"].ToString()
but this is throwing object reference exception.
my routing look like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional });
Thanks in advance.
So after you are redirecting to the Index action of your Profile controller, make sure your action takes in a int parameter like below and return it to the Profile view:
public ActionResult Index(int id)
{
return View(id);
}
Render your links in the Profile's Index view like below:
#model int
<nav class="main">
<ul class="menu" >
<li>#Html.ActionLink("Profile", "Index", new { Id = #Model })</li>
<li>#Html.ActionLink("Configuration", "Index",new { Id = #Model })</li>
<li>#Html.ActionLink("Transaction", "Index", new { Id = #Model })</li>
</ul>
</nav>
Let me know if you need more clarification.
I have the next foreach in my cshtml page, it allows to me iterate in each Model item
#foreach (var item in Model)
{
<div class="date">
#item.pubDate
</div>
<a href="#Url.RouteUrl("Details", new { action = "Details", controller = "News", id = item.id, title = item.title })">
<img src="some route" alt="some alt" />
</a>
}
so now it's working fine and each element inside foreach loop has an url with something like
http://something.com/News/Details/1/first-title
http://something.com/News/Details/2/second-title
It's possible create urls with something like
http://something.com/News/Details/first-title
http://something.com/News/Details/second-title
but i can still sending id parameter to my controller ?
Thanks in advance
Add another route:
routes.MapRoute(
name: "NewsTitleRoute",
url: "News/Details/{id}/{title}",
defaults: new {
controller = "News",
action = "Details",
id = UrlParameter.Optional,
title = UrlParameter.Optional
}
);
In your controller declare the details method like this, with the two parameters as optional:
public ActionResult Details(int? id, string title="")
{
}
That route configuration and Details method will work for:
http://something.com/News/Details/
http://something.com/News/Details/1
http://something.com/News/Details/first-title
http://something.com/News/Details/1/first-title
You can just pass the title property for id parameter.
#Url.RouteUrl("Details",
new { action = "Details", controller = "News", id = item.title})
Then set type of id parameter as string in your action method:
public ActionResult Details(string id)
Or you can create custom route like in von-v's answer. Just make sure it's above the default route.
<li class="rtsLI" id="Summary"><span class="rtsTxt">Test</span></li>
Above I am replacing with following actionlink:
<li class="rtsLI" >#Html.ActionLink("test1", "Index", new { Area = "Area1", Controller = "controller1" }, new { #class = "rtsLink rtsTxt"})</li> "
At first css is working fine. But when using Actionlink, css not working. Thanks
The standard ActionLink helper always HTML encodes the link text. This means that you cannot use it if you want to render HTML inside. You have 3 possibilities:
Modify your CSS so that you don't need a span inside the link and so that the rtsTxt class could directly be applied to the link
Write a custom ActionLink helper that doesn't HTML encode the text and which would allow you to generate the same markup:
public static class ActionLinkExtensions
{
public static IHtmlString ActionLinkUnencoded(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
object routeValues,
object htmlAttributes
)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var link = new TagBuilder("a");
link.MergeAttributes(new RouteValueDictionary(htmlAttributes));
link.Attributes["href"] = urlHelper.Action(actionName, routeValues);
link.InnerHtml = linkText;
return new HtmlString(link.ToString());
}
}
and then:
<li>
#Html.ActionLinkUnencoded(
"<span class=\"rtsTxt\">User Security</span>",
"index",
new { area = "Tools", controller = "UserSecurity" },
new { #class = "rtsLink" }
)
</li>
Use the Url.Action helper:
<li class="rtsLI">
<a href="#Url.Action("index", new { area = "Tools", controller = "UserSecurity" })" class="rtsLink">
<span class="rtsTxt">User Security</span>
</a>
</li>
Best option will be to use #Url.Action extension method
<li class="rtsLI" id="Summary"><span class="rtsTxt">User Security</span></li>
Write code this way:
<li class="rtsLI" >#Html.ActionLink("<span class='rtsTxt'>User Security</span>", "Index", new { Area = "Tools", Controller = "UserSecurity" }, new { #class = "rtsLink"})</li>`
Hi I am having difficulties using Url.Action method, please see my code below, what am I doing wrong....? (I'm using MVC Razor)
<a href='<%: #Url.Action("Edit", "Student",
new { id = item.DealPostID }) %>'>Hello </a>
Student is my StudentController and Edit is ActionResult method.
Remove <%: %> from your Razor view. Those are WebForms tags.
<a href='#Url.Action("Edit", "Student",
new { id = item.DealPostID })'>Hello </a>
Try this:
#Html.ActionLink("Hello", "Edit", "Student", new { id = item.DealPostID }, null)
Argument 1: Link text
Argument 2: Action name
Argument 3: Controller name
Argument 4: Route values
Argument 5: HtmlAttributes. This is set to null so that it doesn't append "?Length=" to your URL.
That should work out for you.
<a href='#Url.Action("Index", "Cliente", "Home")'>
I have this in my Global.asax.cs:
routes.MapRoute(
"User",
"User/{username}/{action}",
new { controller = "User", action = "Index", username = "*" }
);
Then on my _Layout.cshtml I have this code:
<ul id="menu">
#if (!String.IsNullOrEmpty(Context.User.Identity.Name))
{
<li>#Html.ActionLink("Home", "Home", new { controller = "User" }, new { username = Context.User.Identity.Name })</li>
}
</ul>
</div>
</div>
The thing is, it will render the link properly the first time it swings through here. (Link will be /User/rob/Home where "rob" is a username. If I navigate elsewhere on the page and then click back on my link, the link is rendered as /User/*/Home. When I step through the code, Context.User.Identity.Name is correct every time.
Am I missing something really basic here? I'm not sure what to search for.
That's exactly what you should expect given that route. You don't specify username in the route values dictionary but in the HTML attributes, so it takes the default from the route, *. You should be using the signature that allows you to specify both the controller and the action as strings with additional route values in the dictionary.
#if (!String.IsNullOrEmpty(Context.User.Identity.Name))
{
<li>#Html.ActionLink("Home", "Home", "User" new { username = Context.User.Identity.Name }, null )</li>
}