-1

我有显示 ThemeGroups 列表中菜单的布局,当我单击其中一个菜单项时,它应该转到显示此 ThemeGroup 中所有主题的页面,但是当我这样做时,会出现此错误:

    Server Error in '/' Application.
    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its
    dependencies) could have been removed, had its name changed, or is 
    temporarilyunavailable.  
    Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /Home/Browse/1

    Version Information: Microsoft .NET Framework Version:4.0.30319;
    ASP.NET Version:4.0.30319.225 

我有给我主题属性的“ThemeModel.cs”,给我“id”和“ThemeGroupName”的“ThemeGroupsModel.cs”,并且在我的项目中有名为“Services”的文件夹,它有“ThemeSrv”类。 CS”。这个类有一个方法和这个代码:

   public List<ThemesModel> getAllTheme(short ThemeGrId)
    {
   List<ThemesModel> ThemeList = new List<ThemesModel>();
        ThemesModel themeTemp;
        using (var context = new EShopThemeDBEntities(idbconnection.ConnStr))
        {
            var ThemesDb = (from o in context.Themes
                            where o.ThemeGroupId == ThemeGrId
                            select o).ToList();
            if (ThemesDb != null)
            {
                foreach (var item in ThemesDb)
                {
                    themeTemp = new ThemesModel();

                    themeTemp.ThemeID = item.ThemeID;
                    themeTemp.ThemeName = item.CodeName;
                    themeTemp.HtmlF = item.Html;
                    themeTemp.JoomlaF = item.Joomla;
                    themeTemp.Image = item.Image;
                    themeTemp.PsdF = item.PSD;
                    themeTemp.UploadDate = item.UploadDate;
                    themeTemp.UnitPrice = (float)item.UnitPrice;
                    ThemeList.Add(themeTemp);
                }
            }
        }
        return ThemeList;
    }

我有具有以下属性的 HomeModel:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using EshopTheme.Services;

   namespace EshopTheme.Models
   {
public class HomeModel
{
    public MenuSrv MenuList { get; set; }
    public IEnumerable<ThemeGroupsModel> ThemeGr { get; private set; }

    public ThemeSrv ThemesByGrId { get; set; }
    public IEnumerable<ThemesModel> AllThemes { get; private set; }

   public HomeModel()
    {
        this.MenuList = new MenuSrv();
        this.ThemeGr = this.MenuList.getAllThemeGroup();

        this.ThemesByGrId = new ThemeSrv();
        this.AllThemes = ThemesByGrId.getAllTheme(2);
    }

这是我在 HomeController 中的操作:

 [HttpPost]
    public ActionResult Browse(short ThemeGroupId)
    {
        var themes = new ThemeSrv();

        return View(themes.getAllTheme(ThemeGroupId));
    }

这些代码在我的“_LayoutMain.cshtml”中,用于在我的所有页面中显示菜单:

   @{EshopTheme.Models.HomeModel hm = new EshopTheme.Models.HomeModel();     
   }
     <ul id="menu">
                @foreach (var item in hm.ThemeGr)
                {

                    <li>
                    @Html.ActionLink(item.ThemeGroupName, "Browse", "Home", 
                    new { id = item.ThemeGroupId }, new { @class = "linkMenu" })

                    </li>
                } </ul>

我为“浏览”操作创建了名为“Brows.cshtml”的视图,以在我单击菜单中的一项(主题组之一,如“运动”)时显示结果,它显示所有主题的列表,名称为组“运动”。

   @{ EshopTheme.Models.HomeModel hm = new EshopTheme.Models.HomeModel();

ViewBag.Title = "Browse";
Layout = "~/Views/Shared/_LayoutMain.cshtml";
   }


   <ul id="themeList">

@foreach (var item in hm.ThemesByGrIds )
{
    <li><a href="@Html.ActionLink(item.ThemeName, "Browse", "home",
 new { id=item.ThemeID })">
        <img src="@item.Image" alt="@item.ThemeName" /><br />
        <span>Theme Name: @item.ThemeName </span>
        <br /><span>Upload date: @item.UploadDate
        </span>
    @*  <span>price: @item.UnitPrice</span>*@
         </a></li>

}

我的问题是什么??感谢您的帮助...

4

1 回答 1

0

您很可能会收到 404,因为您的路由配置错误和/或您正在尝试GET操作POST结果。试试这个

路线

routes.MapRoute(
      name: "Browse",
      url: "{controller}/{action}/{themeGroupId}",
      defaults: new { controller = "Home", action = "Index", themeGroupId = UrlParameter.Optional }
);

动作结果

public ActionResult Browse(short themeGroupId)
{
    var themes = new ThemeSrv();
    return View(themes.getAllTheme(themeGroupId));
}

您需要告诉路由引擎您要传递给ActionResult

于 2012-09-14T10:34:28.163 回答