目標
プロパティとその値をオブジェクトに「注入」します。
問題
Entity Framework 5 によって作成された次のオブジェクトがあります。
public partial class getSpecificProductToShoppingList_Result
{
public string productName { get; set; }
public string measureAbbreviation { get; set; }
}
セッションを使用して買い物リストを作成しています。ユーザーが自分のリストに何かを追加すると、アプリケーションはこの製品に関する情報を取得するプロシージャを呼び出し、これらの情報はそれぞれ上記のプロパティに入力されます。
ポイントは、ユーザーは必要な量の製品を追加できるということです。これはデータベースの責任ではありません。
製品を追加するには、ユーザーは にアクセスする必要がありますMyApp.com/Products/Add?productId=1&quantity=5
。これを使用して、製品に関するすべての情報を取得できます。
ShoppingListController.cs :
public ActionResult Add(Nullable<int> productId, int quantity)
{
if (Session["ShoppingList"] == null)
{
Session["ShoppingList"] = new
List<getSpecificProductToShoppingList_Result>();
}
getSpecificProductToShoppingList_Result product =
Products.BuildItemToShoppingList(productId);
((List<getSpecificProductToShoppingList_Result>)Session["ShoppingList"])
.Add(product);
return View("Index");
}
そして、ビュー:
@foreach (var item in
(List<MyApp.Models.Data.getSpecificProductToShoppingList_Result>)
Session["ShoppingList"])
{
<p>@item.productName | @item.measureAbbreviation</p>
}
私の質問は簡単です。どうすれば次のようなものを追加でき@item.quantity
ますか?
私が考えていること
私はこのようなことを考えています(製品変数宣言の後の行に注意してください):
public ActionResult Add(Nullable<int> productId, int quantity)
{
if (Session["ShoppingList"] == null)
{
Session["ShoppingList"] =
new List<getSpecificProductToShoppingList_Result>();
}
getSpecificProductToShoppingList_Result product =
Products.BuildItemToShoppingList(productId);
((List<getSpecificProductToShoppingList_Result>)Session["ShoppingList"])
.Insert(productId,
new KeyValuePair<string, int>("quantity", quantity))
.Add(product);
return View("Index");
}
しかし、もちろん、成功はありません。構文は間違っていますが、単に説明するためのものです。
詳細
C#.Net + MVC 4 + Razor エンジンを使用しています