Still getting my feet wet with asp.net mvc. I have a working Action and httppost action but I want to replace the "iffy" code with a RedirectToAction call because the code is rather large for what it does. A call using RedirectToAction would clean it up more. Every way I've tried it fails to work for me in that the drop down list fails to have the proper item selected. The code below works fine but calling RedirectToAction the way I have been does not work for me. So how can i rework the code below to use RedirectToAction ?
I find this line of code particularly troubling because there is no garentee that the "this.Url.RequestContext.RouteData.Route" property will be of type "System.Web.Routing.Route".
// get url request var urlValue = "/" + ((System.Web.Routing.Route)(this.Url.RequestContext.RouteData.Route)).Url;
I also find the second piece of code rather bloated ...
// build the url template urlValue = urlValue.Replace("{realm}", realm); urlValue = urlValue.Replace("{guild}", guild); urlValue = urlValue.Replace("{date}", date.ToShortDateString().Replace("/", "-")); urlValue = urlValue.Replace("{pageIndex}", pageIndex.ToString()); urlValue = urlValue.Replace("{itemCount}", itemCountToDisplay.ToString());
The route I have setup is
routes.MapRoute( "GuildOverview Realm", // Route name "GuildMembers/{realm}/{guild}/{date}/{pageIndex}/{itemCount}", // URL with parameters new { controller = "GuildMembers", action = "Index" }); // Parameter defaults
The code for my controller actions is below ...
[HttpPost]
public ActionResult Index(string realm, string guild, DateTime date, int pageIndex, int itemCount, FormCollection formCollection)
{
// get form data if it's there and try parse num items to display
var cnt = this.Request.Form["ddlDisplayCount"];
int itemCountToDisplay = 10;
if (!string.IsNullOrEmpty(cnt)) int.TryParse(cnt, out itemCountToDisplay);
// get url request
var urlValue = "/" + ((System.Web.Routing.Route)(this.Url.RequestContext.RouteData.Route)).Url;
// build the url template
urlValue = urlValue.Replace("{realm}", realm);
urlValue = urlValue.Replace("{guild}", guild);
urlValue = urlValue.Replace("{date}", date.ToShortDateString().Replace("/", "-"));
urlValue = urlValue.Replace("{pageIndex}", pageIndex.ToString());
urlValue = urlValue.Replace("{itemCount}", itemCountToDisplay.ToString());
return this.Redirect(urlValue);
}
public ActionResult Index(string realm, string guild, DateTime date, int pageIndex, int itemCount)
{
// get the page index
ViewData["pageIndex"] = pageIndex;
// validate item count
var pageItemCountItems = new[] { 10, 20, 50, 100 };
if (!pageItemCountItems.Contains(itemCount)) itemCount = pageItemCountItems[0];
// calc the number of pages there are
var numPages = (this._repository.GetGuildMemberCount(date, realm, guild) / itemCount) + 1;
this.ViewData["pageCount"] = numPages;
// get url request
var urlValue = "/" + ((System.Web.Routing.Route)(this.Url.RequestContext.RouteData.Route)).Url;
// build the url template
urlValue = urlValue.Replace("{realm}", realm);
urlValue = urlValue.Replace("{guild}", guild);
urlValue = urlValue.Replace("{date}", date.ToShortDateString().Replace("/", "-"));
urlValue = urlValue.Replace("{pageIndex}", "{0}");
urlValue = urlValue.Replace("{itemCount}", itemCount.ToString());
// set url template
ViewData["UrlTemplate"] = urlValue;
// set list of items for the display count dropdown
var itemCounts = new SelectList(pageItemCountItems, itemCount);
ViewData["DisplayCount"] = itemCounts;
return View(_repository.GetGuildCharacters(date, realm, guild, (pageIndex - 1) * itemCount, itemCount));
}
and my Index view contains the fallowing
<%=Html.SimplePager(int.Parse(ViewData["pageIndex"].ToString()),
int.Parse(ViewData["pageCount"].ToString()),
ViewData["urlTemplate"].ToString(),
"nav-menu")%>
<% using (Html.BeginForm())
{ %>
<%= Html.DropDownList("ddlDisplayCount", (SelectList)ViewData["DisplayCount"], new { onchange = "this.form.submit();" })%>
<% }%>