I'm trying to implement some simple paging, based on How do I do pagination in ASP.NET MVC?
The paging works fine.
However, I'm now trying to create previous and next links, but can't figure out how to access the params:
My route looks like:
routes.MapRoute(
"Name",
"Controller/ActionName/{pageID}",
new { controller = "Controller", action = "ActionName" , pageID = 0 },
new { pageID = #"\d*"}
);
And my next link looks like:
<%=Html.ActionLink("next page", "ActionName", "Controller", new {pageID = pageID + 1 }, null) %>
The error I get is:
Compiler Error Message: CS0103: The name 'pageID' does not exist in the current context
How should I create the Prev/Next links (or, in this case, just the next)?
The error is occurring on the second PageID in
new {pageID = pageID + 1 }, ...
If you want to reference pageID in this way, you have to pass it in as part of your model.
Have a look at the following tutorial:
NerdDinner Step 8: Paging Support
http://nerddinnerbook.s3.amazonaws.com/Part8.htm
Related
I am using MVC and my requirement is that i need to build a action link with dynamic action route value.
for example.
In my model i have the list<string> so action link should be.
../ActionMethodName/ControllerName? Item[0]=model.item[0]&Item[1]=model.Item[1]...so on.
Any Suggestion is welcome.
Several ways.
1) You can pass as array and access it.
3) You can use foreach with razor syntex to generate this type of link.
Initally , the parameter needs to be passed are hardcoded than OnClick of somthing , replace the queryString values .
#Ajax.ActionLink("Test", "actionName", "ControllerName", new { foo = "foo1", name = "ABC" }, new AjaxOptions { UpdateTargetId = "TargetName" }, new { id = "Test" })
and
$("#btnTest").click(function () {
// Replace the hardcoded parameter value desired one ... :)
$("#Test").attr('href', $("#Test").attr('href').replace("foo1", "XXX"))
});
Note:- this solution i read from asp.net forums
http://forums.asp.net/t/1841591.aspx?+pass+Javascript+value+parameter+with+Ajax+ActionLink
Greetings,
I have an asp.net mvc application. I have some links that corresponds to clients names. When user clicks on this link I would like to show an information of clicked client and additionally a textarea where user shall be able to write some text (comment) about selected client. How can I achieve it?
EDIT
I've made something like:
<%=Html.ActionLink(operatorWhoAnswered.Operator.FirstName, "ShowSingleConverstationWithAnswerForm", "MyMessages", new { id = operatorWhoAnswered.Operator.ROWGUID }, new AjaxOptions() { UpdateTargetId = "ss" }) %>
and my controller action looks as follows:
public PartialViewResult ShowSingleConverstationWithAnswerForm(string id)
{
SingleConversationWithAnswerFormViewModel vm = new SingleConversationWithAnswerFormViewModel();
PartialViewResult viewResult = new PartialViewResult();
viewResult.ViewName = "SingleConverstationWithAnswerForm";
viewResult.ViewData = new ViewDataDictionary(vm);
return viewResult;
}
but view opens in a new page, instead of div with id="ss"
EDIT2
Solution found! I don't know why I have used Html.ActionLink. Ajax.ActionLink works fine!
Try something like this:
Create a div that should be rendered when the user clicks. Name is something lika blabla. Then where your link is you have something like
<%=Ajax.ActionLink("Click here", "Action", "Controller", new { id = "some test data passed in"}, new AjaxOptions() { UpdateTargetId = "blabla" })%>
And let that action return your view
Being rather new to ASP.MVC I'm looking for a solution to the following routing problem.
I want these Url's to lead to the shown pages:
/Member/123/A+Strange+Username -> page with members details
/Member/123 -> as above
/Member/Connections/123 -> page with list of members connections
/Member/Connections/123/A+Strange+Username -> as above
/Member/Comments/123 -> page with list of members comments
/Member/Comments/123/A+Strange+Username -> as above
The username should be ignored but will be appended to links to help search engines.
I have tried with the following routes:
routes.MapRoute("MemberPage", "Member/{id}/{*name}", new { controller = "Member", action = "Details", id = "" });
routes.MapRoute("MemberAction", "Member/{action}/{id}/{*name}", new { controller = "Member", action = "Details", id = "" });
But it seems it always defaults to the first route, and then gets an error since "Connections" or "Comments" is invalid id's for the Details controller.
Is there a way to switch route depending on the type of the id-value, or another way to solve this?
It might help if you add a route constraint to the {id} both routes.
routes.MapRoute("MemberPage", "Member/{id}/{*name}",
new { controller = "Member", action = "Details", id = "" },
new { id=#"\d+" });
routes.MapRoute("MemberAction", "Member/{action}/{id}/{*name}",
new { controller = "Member", action = "Details", id = "" },
new { id=#"\d+" });
This way, it won't try to map "Comments" to {id} in the first route and will fall through to the second one which should work fine with that URL.
I am building a Help Desk Ticket system for a client using ASP.NET MVC 1.0 / C#. I have implemented Steven Sanderson's "App Areas in ASP.NET MVC, Take 2" and it is working great.
In my Globabl.asax page I have some routes defined as such:
public static void RegisterRoutes(RouteCollection routes)
{
// Routing config for the HelpDesk area
routes.CreateArea("HelpDesk", "ProjectName.Areas.HelpDesk.Controllers",
routes.MapRoute(null, "HelpDesk/{controller}/{action}", new { controller = "Ticket", action = "Index" }),
routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details", TicketId = "TicketId" })
);
}
So, if I enter "http://localhost/HelpDesk/Ticket/Details/12" in the browser address bar manually, I get the results I expect. Here is my controller:
public ActionResult Details(int TicketId)
{
hd_Ticket ticket = ticketRepository.GetTicket(TicketId);
if (ticket == null)
return View("NotFound");
else
return View(ticket);
}
In my view I have:
<%= Html.ActionLink(item.Subject, "Details", new { item.TicketId } )%>
But that code generates "http://localhost/HelpDesk/Ticket/Details?TicketId=12" which also returns the expected results. My Question is...
How do I define an ActionLink when using Steven Sanderson's Areas that will create a clean URL like: "http://localhost/HelpDesk/Ticket/Details/12" ?
Try
<%= Html.ActionLink(item.Subject, "Details", new { TicketId = item.TicketId } )%>
The ActionLink method expects a dictionary with keys that match the parameter names. (Note that passing an anonymous object is a convenience for this). Anything else I believe it will just tag onto the end of the URL.
EDIT: The reason that this isn't working for you is because your first route matches and takes precedence (controller and action), but defines no TicketId parameter. You need to switch the order of your routes. You should always put your most specific routes first.
Try
<%= Html.ActionLink(item.Subject, "Details", new { TicketId=item.TicketId } )%>
I think Womp has it ...
Oh and while you are swapping your routes try
routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details"})
I think the , TicketId = "id" is messing things up
Hope that helps,
Dan
I read this article about how you can prefix routes in ruby on rails. I want to be able to do the same thing with asp.net mvc
So I want to be able to define a route like :
/photographers/1/photos/2 //photo 2 of photographer with id 1
/photographers/1/photos //all of photographer with id 1
Any tips ?
EDIT:
"photographers/{id}/photos/{photoID}" - seems to do the job quite ok, BUT how can I support
RedirectToAction<PhotosController>(x => x.Add());
I would like to redirect to : /photographers/1/photos/add
Define your route like this:
routes.MapRoute(
"Photographers",
"photographers/{id}/photos/{photoID}",
new { controller = "Photographers", action = "Photo", photoID = null });
Then define your controller action like this:
public ActionResult Photo(int id, int? photoID)
{
// If photoID is not null, show just that photo.
// Otherwise, show all photographs.
}
You could use regex routing or use wildcards in your routing table so that the {id*} matches the /1/photos/2 for the photographers default controller, parse the string, and redirect to an appropriate action.
Also take a look at this post about nested resources.
RouteTable.Routes.Add(
new Route { Url = "events/[eventId]/tickets/[action]/[id]",
Defaults = new { controller = "Tickets",
action = "List", id = (string)null },
RouteHandler = typeof(MvcRouteHandler) });