超文本标记语言ActionLink将我重定向到错误的控制器
本文关键字:错误 控制器 重定向 ActionLink 超文本标记语言 | 更新日期: 2023-09-27 18:02:21
所以,基本上网站向用户展示了一个下拉列表来选择一个类别。下面的操作用于让用户选择一个类别并显示项目:
public ActionResult SelectResourceCategory()
{
return View(new ResourceModel());
}
[HttpPost]
public ActionResult SelectResourceCategory(ResourceModel rm)
{
Resource r = new Resource();
r.CategoryID = rm.Categories.Id;
List<Resource> GetCourses = new UserBL().GetResourcesByCategoryID(r.CategoryID);
var vRM = new List<ResourceModel>();
foreach (Resource rs in GetCourses)
{
ResourceModel frm = new ResourceModel();
frm.ResourceID = rs.Id;
frm.Title = rs.Title;
frm.Description = rs.Description;
frm.Price = (float)rs.Price;
vRM.Add(frm);
}
return View("ShowResources", vRM);
}
public ActionResult ShowResources()
{
return View();
}
用户选择后,网页将向用户显示用户可以购买的可用资源列表。使用下面的链接,我将所选项目的resourceID作为参数传递:
@Html.ActionLink("Buy", "BuyResource", new RouteValueDictionary(new { id = item.ResourceID }))
和"BuyResource"动作执行以下操作:
public ActionResult BuyResource(int id)
{
Session["Rid"] = id; //GOTO: RedirectFromPayPal
Resource r = new UserBL().GetResourceByID(id);
return View(r);
}
问题:当用户点击"购买"时,由于某种原因,页面将用户重定向到他需要选择类别的页面,特别是"SelectResourceCategory"操作。它应该重定向到"BuyResource"动作,我不明白为什么它不这样做。
编辑:SelectResourceCategory VIEW:
@model SSD.Models.ResourceModel
@{
ViewBag.Title = "SelectResourceCategory";
Layout = "~/Views/Shared/UserPage.cshtml";
}
<h2>SelectResourceCategory</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ResourceModel</legend>
<div class="editor-field">
@Html.DropDownListFor(model => model.Categories.Id, Model.CategoryList)
@Html.ValidationMessageFor(model => model.Categories.Id)
</div>
<p>
<input type="submit" value="Show available resources" />
</p>
</fieldset>
}
路由设置:public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
尝试包含目标控制器的名称:
@Html.ActionLink("Buy", "BuyResource", "YOUR CONTROLLER NAME",
new { id = item.ResourceID })
还要确保项目。ResourceID可转换为int
更新:
在你的:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
id=""
是不必要的,它可能会干扰你的路由。您可能需要考虑使用默认实现:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
正如我在你的问题下面的评论中所写的。
你可以试试:
@Html.ActionLink("Buy", "BuyResource", new { id = item.ResourceID })
或
@Html.ActionLink("Buy", "BuyResource", new RouteValueDictionary{ { id = item.ResourceID } })
注意:
new RouteValueDictionary(new { id = item.ResourceID })
语法可能是错误的,我无法验证这是否是问题的根源,但在传递动作参数时使用匿名类总是更好。
其他一些更复杂的例子,如果你愿意:
@{
var routeValues = new RouteValueDictionary();
routeValues.Add("id", item.ResourceID);
}
@Html.ActionLink("Buy", "BuyResource", routeValues)
确保item.ResourceID
的类型是int
,并且不为空。如果它满足这些要求,它应该100%工作