还在用asp.net mvc弄湿我的脚。我有一个有效的Action和httppost动作,但是我想用RedirectToAction调用替换“iffy”代码,因为它的代码相当大。使用RedirectToAction的呼叫会更多地清理它。我尝试过的每一种方式都无法为我工作,因为下拉列表无法选择正确的项目。下面的代码工作正常,但以我的方式调用RedirectToAction对我不起作用。那么我如何重新编写下面的代码才能使用RedirectToAction?
我发现这行代码特别令人不安,因为没有人知道“this.Url.RequestContext.RouteData.Route”属性的类型为“System.Web.Routing.Route”。
// 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());
我设置的路线是
routes.MapRoute( "GuildOverview Realm", // Route name "GuildMembers/{realm}/{guild}/{date}/{pageIndex}/{itemCount}", // URL with parameters new { controller = "GuildMembers", action = "Index" }); // Parameter defaults
我的控制器操作的代码低于......
[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));
}
我的索引视图包含了以下内容
<%=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();" })%>
<% }%>
答案 0 :(得分:0)
您可以在案例中使用重定向操作,如下所示:
return RedirectToAction("Index", new {realm = realm, guild = guild,
date = date, pageIndex = 0,
itemCount = itemCount}) ;
DropDownList选择失败不是由于您的路由,而是因为ViewData仅存在于当前请求中。您应该使用TempData。请参阅:ASP.NET MVC: return Redirect and ViewData