views:

203

answers:

3

i have tried till my head hurts and have been reading so much my eyes now bleed.

i have a controller, and i want to get one set of data for the order and one list for the order items.

i have created a order class and a order items class, and trying to get it so that the order will have a list of order items but it is crashing my brain i dont know if im on the right path or not or if im missing something i just dont get it.

here is my code

public ActionResult finaliseQuote(string quoteid)
    {
        ViewData["quoteid"] = quoteid;

        // populate our class with data about our quote so far
        var orders = (from q in quote.All()
                      where q.quoteid == quoteid
                      select q).SingleOrDefault();



        //IList<quoteParts> orderItemsList = new quoteParts(orderitems); (this not work)

        quoteParts myparts = new quoteParts((from qi in quoteItem.All()
                          where qi.quote_id == quoteid
                          select qi).SingleOrDefault()); // compiles but this is not a list ?????

        return View();
    }

then my 2 classes:

    public class quoteInfo
{
    public IList<quoteParts> items { get; set; }
    public DateTime datecreated { get; set; }
    public double totalcost { get; set; }
    public string quotesid { get; set; }

    public quoteInfo(quote myquote)
    {
        items = new List<quoteParts>();
    }
}

public class quoteParts
{
    public string itemsid { get; set; }
    public bool isextra { get; set; }
    public string qty { get; set; }
    public string mx { get; set; }
    public string my { get; set; }
    public string prodid { get; set; }

    public quoteParts(quoteItem item)
    {
        itemsid = item.itemid;
        isextra = item.isextra;
        qty = item.qty;
        mx = item.measureX.ToString();
        my = item.measureY.ToString();
        prodid = item.prodid;
    }
}

i am hoping that i can create a view and then reference quoteInfo and also loop through quoteParts in another section of the page.

i know to some this may seem like i am being lazy but im not i learn when i get it right once it works it is then that i understand, right now i want to do OOP etc but seem to be hitting wall after wall.

many thanks

+5  A: 

You need to create a ViewModel class for your view, that can hold the data:

public class QuoteViewModel
{
     public QuoteInfo Info { get; set; }
     public QuoteParts Parts { get; set; }
     public int QuoteId { get; set; }
}

Meke your View strongly typed to this class, and create an instance of it in your action method that you return with the View:

public ActionResult finaliseQuote(string quoteid)
{
    var info = new QuoteInfo(
                  (from q in quote.All()
                   where q.quoteid == quoteid
                   select q).SingleOrDefault()
               );
    var parts = new QuoteParts(
                  (from qi in quoteItem.All()
                   where qi.quote_id == quoteid
                   select qi).SingleOrDefault()
               );

    var model = new QuoteViewModel { 
                        QuoteId = quoteid,
                        Info = info,
                        Parts = parts 
                    };

    return View(model);
}

Notice that I changed the casing of your class names to reflect C# common practice. '

In your View, you render the different items by accessing either ViewData.Model.Info, ViewData.Model.Parts or ViewData.Model.QuoteId. (Yes, the capital M in Model is intentional...)

Also, I can't vouch for that this code will work off the shelf - I don't know what parts of your code worked before...

Tomas Lycken
thanks very good i understand that, but on the parts bit i get error:system.linq.IQueryable<stone_center.MOdel.quoteItem> is not assignable to param stone_center.Models.quoteItem?????
minus4
Sorry, a small mistake of mine. I have updated the code (added a `.SingleOrDefautl()` on LINQ cal - see revision history to see exactly what I did)
Tomas Lycken
+1 Nice simple example.
CmdrTallen
thanks for your help on this, i have it all together now
minus4
+1  A: 

QuoteParts constructor takes an object of type quoteItem. However in finaliseQuote method, you're passing an IQueryable to the constructor. They're incompatible types hence the error you're seeing. You will either need to select a single item in your LINQ query or change QueryParts to handle a collection of quoteItems.

Also your view model is not representative of what you want to do. You want an order and a list of items that belong to that order. Your original code and QuoteViewModel added in later post deal with a single order and a single order item. You want to make changes so that your view model has a list of order items. Take a look at the code snippet below.

public class QuotePart
{
  public int ProdId{get;set;}
  public string ItemId{get;set;}
  public string Quantity{get;set;}
}


model.Parts = from qi in quoteItem.All()
              where qi.quote_id == quoteid
              select new QuotePart{ ProdId = qi.prodid, ItemId = qi.itemid,Quantity=qi.qty};

Note that Parts is now a collection. You will need to change the type of Parts property on QuoteViewModel from QuoteParts to IEnumerable<IQuotePart>.

Mehmet Aras
thanks for your comments, and as right as they are this is my muddle cos i know in theory what neds to be done but im jumping from asp classic none OOP based to DAL, OOP etc all at once and trying to pick up really fast. i was first thinking that my quoteInfo would actually hold a list.am gonna try the ienumerable on parts and reverse some code back to how you say and se what happensthanks
minus4
okay think i have got it have changed parts to IQueryableand jiggled a few things and all seems fine, BUT i get error on single = results.ToList()[0]; this is in SingleOrDefault cannot convert string to int ?????
minus4
Did IEnumerable<QuotePart> not work? What is the type of single and what's in results? It seems like you are trying to assign a string value to an integer. If the string holds an integer value and you want to assign it to a type of int, then you will need to parse the string. Take a look at int.TryParse method.
Mehmet Aras
IEnumerable did not work no, or i dident know how so went with iqueryable, as for the error its way beyond my scope as its subsonic classes and i created a test view and just at it to get all quotes and i got same error, i have posted it as an issue on git as i have had a look and cant see anything in the database or the class that might be causing the problem
minus4
A: 

okay i have taken on board everything so far and i have have fixed the bugs in subsonic

here is how it stands

public ActionResult finaliseQuote(string quoteid)
    {
        ViewData["quoteid"] = quoteid;

        quoteid = "62";

        var model = new QuoteViewModel { QuoteId = Convert.ToInt32(quoteid) };


        quote orders = quote.SingleOrDefault(x => x.quoteid == 62);

        model.Info = new QuoteInfo(orders.datecreated, orders.totalcost);

        model.Parts = from qi in quoteItem.All()
                      where qi.quote_id == 62
                      select new QuoteParts(qi.prodid, qi.itemid, Convert.ToInt32(qi.qty), qi.measureX.ToString(),
                                         qi.measureY.ToString(), qi.isextra);

        return View(model);

    }

and here are the classes

public class QuoteViewModel { public QuoteInfo Info { get; set; } public IEnumerable Parts { get; set; } public int QuoteId { get; set; } }

public class QuoteInfo
{
    public DateTime datecreated { get; set; }
    public double totalcost { get; set; }
    public string quotesid { get; set; }

    public QuoteInfo(DateTime Datecreated, Double TotalCost)
    {
        datecreated = Datecreated;
        totalcost = TotalCost;
    }
}

public class QuoteParts
{
    public int itemsid { get; set; }
    public bool isextra { get; set; }
    public int qty { get; set; }
    public string mx { get; set; }
    public string my { get; set; }
    public int prodid { get; set; }

    public QuoteParts(int Prodid, int Itemsid, int Qty, String Mx, String mY, bool IsExtra)
    {

        itemsid = Itemsid;
        isextra = IsExtra;
        qty = Qty;
        mx = Mx;
        my = mY;
        prodid = Prodid;
    }
}

however when i run i get this error:

The model item passed into the dictionary is of type 'stone_center.Controllers.QuoteViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[stone_center.Controllers.QuoteViewModel]'.

thanks

minus4