2012-12-28 49 views
0

我是新的ASP.Net MVC和MVC體系結構。我正在使用數據庫代碼優先方法構建一個簡單的應用程序。基本的ASP.NET MVC查詢參數

我有一個名爲cookId的屬性的食譜模型,它是創建食譜的用戶的ID。

現在我希望能夠將查詢字符串傳遞到我的頁面,並獲得cookId與參數相同的食譜,並在我的視圖中列出我。

我該如何做到這一點?我應該把這個邏輯放在哪裏?在我的controller或我的view

回答

2

那麼,asp.net mvc的作品與路線,或TableRoutes。默認路由使用以下格式創建:{controller}/{action}/{id}

因此,當您收到您的動作請求時,您可以從您的動作(在控制器上)的id參數中檢索此ID,並使用此值命中您的數據庫並獲取需要在視圖上顯示的所有記錄。你可以嘗試一些liek這樣的:

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"]拿到身份證,但它不是在asp.net mvc的一個很好的實踐。你可以在你的動作中使用參數並使用它。

在你看來,你可以用IEnumerable<Recipe>輸入並顯示它在桌子上,像這樣:

@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將以下routetable設置好的在Global.asax的渲染a標籤與apropriated路線。如果還有其他參數要傳遞到查詢字符串中,那麼也可以像在another參數的示例中那樣添加它。

+2

差不多,但不完全。使用默認路由,您應該將該ID作爲單個對象的ID。如果網址是Home/Cooks/5,則5應該是廚師的ID。類似的,如果網址是Home/Recipes/3,3應該代表一個配方,而不是所有廚師3的食譜。Home/Recipes /?cookId = 5應該返回一個廚師的所有食譜,其中5代表id。 –

+0

是@seanwoodward你是對的+1 :)。我認爲在語義上我們可以使用id參數來做一個友好的url。我不確定這是不是最好的方法,但它對我來說工作正常。 –

1

從不把邏輯放在視圖中。該視圖應該只顯示模型中提供的信息。將邏輯放入控制器中。

控制器:

[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> 
} 
1

控制器:

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

視圖(例如視圖\ yourController \ GetRecipes.cshtml),僅使用此文件來顯示數據,它不建議在這裏放置邏輯:

@model List<Namespace.Recipe> 

<h2>Recipes</h2> 

@foreach(var r in Model) 
{ 
    <p>r.Name</p> 
} 

這將以下查詢字符串被稱爲:

/Recipes/GetRecipes?cookId=SomeId 
1

您可能有一個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); 
    }