c# - How to implement an MVC Model that doesn't link directly to a database -


i trying implement view displays information database has been modified before displaying. example, trying add markup labor , hardware costs give final price. problem can't use foreach loop display in view because list, , having trouble declaring ienumerable because won't let me use add() function. here model,

namespace myname.models{ using system; using system.collections.generic;  public partial class pricing {     public list<int> id { get; set; }     public list<string> manufac { get; set; }     public list<string> model { get; set; }     public list<string> service { get; set; }     public list<string> type { get; set; }     public list<int> price { get; set; }  } 

here index method in controller,

 private wtentities db = new wtentities();      // get: /jobinformation/     [authorize(roles = "employee")]     public actionresult index()     {         pricing prices = new pricing();         prices.id = new list<int>();         prices.manufac = new list<string>();         prices.model = new list<string>();         prices.price = new list<int>();         prices.service = new list<string>();          foreach(var item in db.jobinformations)         {             prices.id.add(item.id);             prices.manufac.add(item.manufac);             prices.model.add(item.model);             prices.service.add(item.service);             prices.price.add(((int)item.markupcostcents + (int)item.laborcostcents + (int)item.hardwarecostcents) / 100);         }         return view(prices);     } 

and here view,

@foreach (var item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.type)         </td>         <td>             @html.displayfor(modelitem => item.manufac)         </td>         <td>             @html.displayfor(modelitem => item.model)         </td>         <td>             @html.displayfor(modelitem => item.service)         </td>         <td>             @html.displayfor(modelitem => item.price)         </td>     </tr> } 

you've designed model incorrectly you're trying accomplish.

you should create viewmodel looks this:

public class pricing {     public int id { get; set; }     public string manufac { get; set; }     public string model { get; set; }     public string service { get; set; }     public string type { get; set; }     public int price { get; set; } } 

then in controller:

var prices = new list<pricing>();  foreach(var item in db.jobinformations) {     var price = new pricing();     price.id = item.id;     price.manufac = item.manufac;     price.model = item.model;     price.service = item.service;     price.price = ((int)item.markupcostcents + (int)item.laborcostcents + (int)item.hardwarecostcents) / 100;      prices.add( price ); }  return view(prices); 

now in view can you've written (just sure change @model declaration list).


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -