How do you set the linktext on url in controller - asp.net-mvc-3

is there a way to create an action link in my controller and set the link text? Can't find an overload...
Url.Action("Edit", "Controller", new { id = myId });

To create a link in controller use Helpers like this:
string link = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Root", "About", "Home", null, null);

Here you go: ActionLink Method
#Html.ActionLink("Edit", "Edit", "Controller", new { id = myId })

Related

ASP.NET MVC Areas Naviation

I have a MVC web application with a logout form like this:
#using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { area = "", id = "logoutForm" }))
{
#Html.AntiForgeryToken()
Log off
}
The problem is that if I have navigated to one of the areas and I click the LogOut button the action does not work.
Does anyone know how to make the above code work with the default area?
Thanks
Gerry
Got it:
#using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
This works!!!!!

Unexpected HTML from Razor

I have some Razor code in a view that is supposed to route through to a different part of my model:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID })
but when I look at the HTML that is emmitted, it is does not relect what I have written:
Edit
How can I stop this from happening?
That's because you are using the wrong overload. It should be like this:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID }, null)
Let's see why you are using the wrong overload. Let's break down what you wrote:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // routeValues
new { id = item.JOURNAL.REF_ID } // htmlAttributes
)
See the problem?
And now let's break down the correct way:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // controllerName
new { id = item.JOURNAL.REF_ID }, // routeValues
null // htmlAttributes
)
See the difference?
I would recommend you reading very carefully the documentation and the different available overloads of the ActionLink helper as well as the exact significance of their parameters.

Html.ActionLink MVC3 ASPX / v. ?=

when I use the following actionlink:
<%: Html.ActionLink("Study Cases", "Index", "CourseCases", new { id = Model.ID }, new { #class = "t-button", #style = "width:240px; color:white; text-align:center" })%>
The url address in the browser is:
http://localhost:11111/CourseCases/Index/9
How can I change it so the url will be
http://localhost:11111/CourseCases?courseId=9
It works when I use:
return RedirectToAction("Index", "CourseCases", new { courseId = id });
in the controller.
Thanks in adance.
Like this:
<%= Html.ActionLink(
"Study Cases",
"Index",
"CourseCases",
new { courseId = Model.ID },
new {
#class = "t-button",
#style = "width:240px; color:white; text-align:center"
}
) %>
The reason why your code generates http://localhost:11111/CourseCases/Index/9 is because the {id} is used by the default route that was generated when you created your ASP.NET MVC 3 application, so when you specify id = Model.ID it will match the route pattern definition in your Global.asax which is {controller}/{action}/{id} thus you get CourseCases/Index/9.

How can I add an anchor tag to my URL?

MVC 3.net I want to add an anchor to the end a url.
I tried to include an anchor query string but the hash '#' changes to %23 or something like that in the url.
Is there a way of working around this?
There is an overload of the ActionLink helper that allows you to specify the fragment:
#Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
will produce (assuming default routes):
Link Text
UPDATE:
and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:
public ActionResult Index()
{
var url = UrlHelper.GenerateUrl(
null,
"Action",
"Controller",
null,
null,
"fragment",
new RouteValueDictionary(new { id = "123" }),
Url.RouteCollection,
Url.RequestContext,
false
);
return Redirect(url);
}

MVC 3 routing problem

In my site i have the following routing table:
routes.MapRoute("Something",
"sites/{company}/{site}/",
new { controller = "Home", action = "LoadClientSite" },
new[] { "CorrectOnLine.Controllers" });
routes.MapRoute("Default1", // Route name
"{company}/{site}/", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
routes.MapRoute("Default2", // Route name
"{company}/{site}/{id}", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
I am using the following link to call an action:
#(Html.Action("index", "home", new { area = "ClientsSites", CompanyID = Model.CompanyID, SiteID = Model.SiteID, CategoryID = Model.CategoryID, Message = Model.Message }))
In the view i have an image:
<img alt="" src="../../../../../WareHouse/Sites/logo_site_#(Model.Site.SiteID).jpg" height="250px" width="750px" />
The problem is that when the image loades MVC tries to locate the image src in the routing table , conntroller = WareHouse, action = Sites.
How can i make MVC to only load the iamge and not try to load it as a view?
Thanks,
Alex
You could ignore route for all paths that end in the .jpg extension...
routes.IgnoreRoute("{*alljpeg}", new {alljpeg=#".*\.jpg(/.*)?"});

Resources