Tuesday 15 June 2010

c# - How to "inject" a property and its value to an object -


target

The property and its value for an object "inject"

Problem

I have the following object that is created by the unit framework 5:

  public partial class getSpecificProductToShoppingList_Result {public string productName {get; Set; } Public string measurement abate {received; Set; }}   

I am creating a shopping list using a session and when a user adds something to their list, the application tells a process to get information about this product And these information is the above mentioned properties respectively.

The issue is: The user can add the amount of the product that he wants and it is not the responsibility of the database.

To add a product, the user should reach MyApp.com/Products/Add?productId=1&quantity=5 . I can get all the information about the product by using it:

ShoppingListController.cs :

  Public activity (Nullable & lt; integer> productId, integer volume) {(session ["shoppingList"] == faucet) {session ["ShoppingList"] New list & lt = if; GetSpecificProductToShoppingList_Result & gt; (); } GetSpecificProductToShoppingList_Result Product = Product.Bildemato Shoppingist (Product ID); ((List & lt; getSpecificProductToShoppingList_Result & gt;) Session ["Shopping List"]) Add (product); See Return ("Index"); }   

And then, view :

  @forach (var item (list & lieutenant ); MyApp.Models.Data.getSpecificProductToShoppingList_Result & gt; Session ["Shopping List"]) {& lt; P & gt; @ Item.productName | @ Item.measureAbex & lt; / P & gt; }   

My question is simple: how can I add something like @ item.quantity

What are I thinking about? / H2>

I am thinking something like this (note the line after the product variable declaration):

  Add public functioning (faucet qualified & gt; product & gt; ID, int volume) {if (session ["shopping list"] == tap) {session ["shopping list"] = new list & lt; GetSpecificProductToShoppingList_Result & gt; (); } GetSpecificProductToShoppingList_Result Product = Product.Bildemato Shoppingist (Product ID); Season ["ShoppingList"]). (Input, new KeyValuePair & lt; string, integer & gt; ("quantity", quantity)) .add (product); See Return ("Index"); }   

but a ???? Of course one no success. The syntax is wrong, but it was just to illustrate.

Description

I am using the C #net + MVC 4 + razor engine

Your organization structure model in the same namespace, i.e. you put the .edmx file to the same folder, the same name is defined as the outline of the unit, which I think in my case is a class of Form a new class with getSpecificProductToShoppingList_Result .

Mark the square partially, and you can add your own properties to it. Example: You have it in your .edmx designer file:

  [EdmEntityTypeAttribute (NamespaceName = "YourModel", name = "getSpecificProductToShoppingList_Result")] [Serializable () ] [DataContractAttribute] Public partial class getSpecificProductToShoppingList_Result: EntityObject {...}   

This is auto generated and you can not change it to add some properties, but It is partially marked so that you can increase it, e.g. If I add: get public partial classfull semiconductorList = reference {get public entity quantity; Set; }}

C # join them together, so in my controller, I can on the new

; GetSpecificProductToShoppingList_Result & gt; ();

something.First () quantity = 50;

The quantity getSpecificProductToShoppingList_Result is not related because that's my database type, but I've added it through a partial organs.

No comments:

Post a Comment