因此,基本上该网站向用户显示一个下拉列表以选择一个类别。以下操作用于让用户选择一个类别并显示项目:
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 }))
和Action“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);
}
答案 0 :(得分:2)
尝试包含目标控制器的名称:
@Html.ActionLink("Buy", "BuyResource", "YOUR CONTROLLER NAME",
new { id = item.ResourceID })
还要确保item.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 }
);
答案 1 :(得分:0)
正如我在你的问题评论中写的那样。
您可以尝试:
@Html.ActionLink("Buy", "BuyResource", new { id = item.ResourceID })
或
@Html.ActionLink("Buy", "BuyResource", new RouteValueDictionary{ { id = item.ResourceID } })
注意:
new RouteValueDictionary(new { id = item.ResourceID })
语法可能有误,我无法验证这是否是您问题的根源,但在传递操作参数时使用匿名类总是更好。< / p>
编辑:
如果您愿意,可以使用其他更复杂的示例:
@{
var routeValues = new RouteValueDictionary();
routeValues.Add("id", item.ResourceID);
}
@Html.ActionLink("Buy", "BuyResource", routeValues)
请确保item.ResourceID
的类型为int
,并且它不为空。如果它符合这些要求,它应该100%工作。