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(/.*)?"});
Related
I have an ASP.NET-Web-API project with a generated help area. I would like to route the home page to be pointing to the help page. How can this be done?
I've tried to modify the area registration like below but it is not working
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"HomePage",
"{controller}/{action}/{id}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
context.MapRoute(
"HelpPage_Default",
"Help/{action}/{apiId}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
HelpPageConfig.Register(GlobalConfiguration.Configuration);
}
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.
I have 2 dropdownlists on the Index page, and I wish to pass the id's of the selected items to the Create Page, so that I can populate the 2 dropdownlists on the Create page the same as the Index page.
Can you please suggest how I can do this?
At the moment I have this in the Index View :-
#Html.ActionLink("Create New", "Create", new { LeagueId = "ddlLeagues" }, new { ClubId = "ddlClubs" })
And then in the Controller :-
public ActionResult Create(int LeagueId, int ClubId)
{
var _LeagueID = LeagueId;
var _ClubID = ClubId;
Any help is very much appreciated!
Thanks
You can do it as described in this post:
ActionLink routeValue from a TextBox
you basically need to wrap your dropdowns with a form that routes to the create function, and the submit will take care of passing those values to your controller because they will be in the form data:
#using(Html.BeginForm("Create", "Footballer", FormMethod.Get))
{
#Html.DropDownList("LeagueId", Model.Leagues)
#Html.DropDownList("ClubId", Model.Clubs)
<input type="submit" value="Create"/>
}
If you are using a strongly typed model that has Properties for LeagueId and ClubId then use:
#Html.DropDownListFor(m => m.LeagueId, Model.Leagues)
#Html.DropDownListFor(m => m.ClubId, Model.Clubs)
Model.Clubs and Model.League are the IEnumerables that you will use to populate your dropDowns ofcourse
in your controller make sure you have the following:
[HttpGet]
public ActionMethod Create(int LeagueId, int ClubId)
{
//return your Create View
}
[HttpPost]
public ActionMethod Create(FormCollection data)
{
//Perform the create here
}
You can add a route into the application RegisterRoutes :
routes.MapRoute(
"CreateFootBallerWith2ComboOptions",
"{controller}/{action}/{LeagueId}/{ClubId}",
new { controller = "Footballer", action = "Create", LeagueId = -1, ClubId = -1 } // Default Values
);
You can then use what Bassam suggest with the ActionLink which is a Html Helper.
#Html.ActionLink("Create New", "Create",
new { LeagueId = 1, ClubId = 213 });
or use directly from the browser using :
localhost:7246/Footballer/Create/1/5
I'm at a loss trying to figure out why I have Actions that are returning 404 'The Resource cannot be found' errors.
Controller Name: ItemManagementController
My Index view has list of items in a table. Each row contains two links, 'Delete' and 'Request Update'. The Delete link calls a Delete action and works fine. The Request Update gives me the 404 error, and seems as if trying to navigate to a URL like http://localhost/TVAPDev/ItemManagement/RequestUpdate?itemID=9.
I have to assume I'm missing something simple, as they are identical in what they do from the view aspect. The actions as defined in the controller are both similar except that they call different methods on a service layer, but that's it.
Here are my two Controller Actions.
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Delete(int itemID) {
var svc = new ItemManagementService(_repository);
var requestModel = svc.GetItemDeleteModel(itemID);
svc.DeleteItem(requestModel);
var message = requestModel.ActionMessage;
return Json(new { id = itemID, ChangeStatus = requestModel.ItemDetails.ItemChangeStatus.ToString(), ChangeType = requestModel.ItemDetails.ItemChangeType.ToString(), message});
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult RequestUpdate(int itemID) {
var svc = new ItemManagementService(_repository);
var requestModel = svc.GetItemUpdateRequestModel(itemID);
svc.RequestItemUpdate(requestModel);
var message = requestModel.ActionMessage;
return Json(new { id = itemID, ChangeStatus = requestModel.ItemDetails.ItemChangeStatus.ToString(), ChangeType = requestModel.ItemDetails.ItemChangeType.ToString(), message });
}
Here are the links as they are defined in the View
<td class="tblist" style="white-space: nowrap;">
#Html.ActionLink("Request Update", "RequestUpdate", new { itemID = item.ItemID }, new AjaxOptions {
HttpMethod = "POST",
Confirm = "Request an Update to this item?",
OnSuccess = "actionCompleted"
})break;
}
</td>
<td class="tblist" style="white-space: nowrap;">
#Ajax.ActionLink("Delete", "Delete", new { itemID = item.ItemID }, new AjaxOptions {
HttpMethod = "POST",
Confirm = "Are you sure you want to delete this Item?",
OnSuccess = "actionCompleted"
})
</td>
Again, the Delete here works without issue. The Request Update link gives me the Http 404 error.
Anyhelp here would be greatly appreciated.
Why are you using AjaxOptions on a normal Html.ActionLink (which is what Request Update is)?
Maybe you wanted it to be like this:
#Ajax.ActionLink(
"Request Update",
"RequestUpdate",
new {
itemID = item.ItemID
},
new AjaxOptions {
HttpMethod = "POST",
Confirm = "Request an Update to this item?",
OnSuccess = "actionCompleted"
}
)
Check your View code... the delete is using the Ajax html helper and the update is using the regular html helper.
I am using MVC3 and have Areas in my application. In general, everything works fine, I can navigate to my area (e.g. Admin=Area, Controller=Department) like this:
<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>
However, what I noticed is that if I don't specify the area in my ActionLink, e.g.
<%: Html.ActionLink("Test", "Index", "Home")%>
This will stop working if I have navigated to the "Admin" area. i.e. my url is now
http://localhost/myproj/Admin/Home/Index
instead of
http://localhost/myproj/Home/Index
Any ideas on what is going on here?
My routes are all the defaults when creating an MVC app / Areas. i.e.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyProj.Controllers" }
);
}
And my area registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Is this by design?
This posting suggests using RouteLink instead of ActionLink:
say to use RouteLink
Is it required to have "area" routevalue on actionlink if the application was grouped into areas?
This StackOverflow answer correctly states that you need to provide the Area name if you're using areas.
eg.
Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })