如何通过其groupName获取项目列表?

时间:2012-09-14 08:13:51

标签: asp.net-mvc-3

我有使用ThemeGroups列表显示菜单的Layout,当我点击其中一个菜单项时,它应该转到显示此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”,它给了我主题的属性,“ThemeGroupsModel.cs”给了我“id”和“ThemeGroupName”,并在我的项目中有一个名为“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”的“浏览”动作创建视图,以便在菜单中点击一个项目(主题组之一,如“sport”)时显示结果,它显示所有主题的列表名称组“sport”。

   @{ 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>

}

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

1 个答案:

答案 0 :(得分:0)

您最有可能获得404,因为您的路由配置错误和/或您尝试GET POST行动结果。试试这个

<强>路线

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

<强>的ActionResult

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

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

的参数