【问题标题】:Basic ASP.NET MVC query with parameter带参数的基本 ASP.NET MVC 查询
【发布时间】:2012-12-28 21:03:16
【问题描述】:

我是 ASP.Net MVC 和 MVC 架构的新手。我正在使用 Database Code First 方法构建一个简单的应用程序。

我有一个配方模型,其属性名为cookId,它是创建配方的用户的 ID。

现在我希望能够将查询字符串传递到我的页面并仅获取 cookId 与参数相同的食谱并在我的视图中列出 i。

我怎样才能做到这一点?我应该把这个逻辑放在哪里?在我的controller 还是在我的view

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework razor


    【解决方案1】:

    好吧,asp.net mvc 与路由或 TableRoutes 一起使用。默认路由使用以下格式创建:{controller}/{action}/{id}

    因此,当您收到有关您的操作的请求时,您可以从您的操作(在控制器上)的 id 参数中检索此 ID,并使用此值访问您的数据库并获取您需要在看法。你可以试试这样的:

    public ActionResult Recipes(string id)
    {
       IEnumerable<Recipe> list = _repository.GetRecipeByCookId(id); // this method should return list of Recipes
    
       return View(list); // return your View called "Recipes" passing your list
    }
    

    您也可以使用Request.QueryString["Id"] 来获取Id,但这在asp.net mvc 中不是一个好习惯。您可以在操作中使用参数并使用它。

    在您的视图中,您可以使用 IEnumerable&lt;Recipe&gt; 键入它并将其显示在表格上,例如:

    @model IEnumerable<Recipe>
    
    <table>
        @foreach(var recipe in Model)
        {
            <tr>
                <td>@recipe.Name</td>           
                <td>@recipe.CookId</td>
                <td>@recipe.OtherProperties</td>
            </tr>
        }
    </table>
    

    要为请求创建一个传递此 ID 的链接,您可以使用 Html.ActionLink,类似于您的视图:

    @Html.ActionLink("Text of You Link", "Action", "Controller", new { id = 5, another = 10 }, new { @class = "css class for you link" });
    

    和asp.net mvc 将呈现a 标记,该标记具有遵循在global.asax 上设置的路由表的专用路由。如果您有其他参数要传入查询字符串,您也可以像我在示例中使用another 参数一样添加它。

    【讨论】:

    • 几乎,但不完全。使用默认路由,您应该将 id 视为单个对象的 id。如果 url 是 Home/Cooks/5,则 5 应该是厨师的 id。同样,如果 url 是 Home/Recipes/3,则 3 应该代表一个食谱,而不是厨师 3 的所有食谱。 Home/Recipes/?cookId=5 应该返回一个厨师的所有食谱,ID 为 5。
    • 是的@seanwoodward 你是对的 +1 :)。我认为在语义上我们可以使用 id 参数来做一个友好的 url。我不确定这是否是最好的方法,但对我来说效果很好。
    【解决方案2】:

    切勿将逻辑放入视图中。视图应该只显示模型中提供的信息。将逻辑放入控制器中。

    控制器:

    [HttpGet]
    public ActionResult Recipes(int cookId)
    {
         var recipes = /* get recipes based on cook */;
         List<RecipeModel> model = recipes
             .Select(r => new RecipeModel
             { 
                Id = r.Id,
                CookId = r.CookId,
                ...
             })
             .ToList();
         return View(model);
    }
    

    查看:

    @model List<RecipeModel>
    
    @foreach (RecipeModel item in Model)
    {
        <div>
            <span>Id:</span>
            <span>@item.Id</span>
        </div>
    }
    

    【讨论】:

      【解决方案3】:

      控制器:

      [HttpGet]
      public ActionResult GetRecipes(int cookId)
      {
          // model can view a List<Recipe>, logic goes here
          var model = SomeQueryThatReturnsRecipesFrom(cookId);
          return View(model)
      }
      

      视图(例如views\yourController\GetRecipes.cshtml),只用这个文件来展示数据,不推荐把逻辑放在这里:

      @model List<Namespace.Recipe>
      
      <h2>Recipes</h2>
      
      @foreach(var r in Model)
      {
          <p>r.Name</p>
      }
      

      这将使用以下查询字符串调用:

      /Recipes/GetRecipes?cookId=SomeId
      

      【讨论】:

        【解决方案4】:

        你可能有一个 CooksController。该控制器将返回厨师列表。该列表可能包括厨师食谱的链接。您的 RecipesController 可以处理对给定 cookId 的所有食谱的请求。

        @Html.ActionLink("Recipes", "RecipesByCook", "Recipes", new { cookId = model.cookId }, null};
        

        以上代码用于视图 Cooks/Index.shtml。它会创建一个链接,该链接使用查询字符串来识别您想要的 cookId。

        RecipesController 将有一个 RecipiesByCook 方法,该方法接受 cookId 的参数。此方法将处理对此类 URL 的请求,Home/Recipies/RecipeByCook?cookId=4。

        然后,您的 RecipesController 可以返回一个 ActionResult,其中包含要显示的正确食谱集。非常简单(因为您可能想要添加更多以显示视图,例如有关厨师的信息):

            public ActionResult RecipesByCook(int cookId)
            {
                var recipes = repository.Recipes.Where(r => r.cookId == cookId);
        
                return View(recipes);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-15
          相关资源
          最近更新 更多