0

I have a website and when you order 2 products they are added in the database so know i have this model

    public IQueryable<CommonLayer.Models.OrdersModel> GetOrderAsModel(Guid id)
    {
        return (from p
                in this.Entities.ORDERS
                join orderdetails in this.Entities.ORDERDETAILS on p.OrderId equals orderdetails.OrderDetailsOrderId
                where p.OrderId == id
                select new CommonLayer.Models.OrdersModel()
                {
                    Orderid = p.OrderId,
                    Orderdetailsid = orderdetails.OrderDetailsOrderId,
                    OrderDate = p.OrderDate,
                    Orderstatus = p.OrderStatus,
                    ProductQty = orderdetails.ProductQuantity,
                    OrderNum = p.OrderNum,
                    Userid = p.UserId,
                    ProductName = orderdetails.PRODUCT.ProductName
                });
    }

   This model return the order along with the order details(if for example i have two products i have two orderdetails)

Now i am trying to add the ability to alter the value of some fields from an order and i am trying to get all products in a specific order, if i have for example 4 products it returns for but they are all exactly like the first product entered even if i have different products, even the quantity if in the order i have 1 of one product and 5 of the other it returns two products same name same quantity. Below code shows the for each i am trying to implement:

@foreach (var item in Model)
{
             <div class="form-group">
                           @Html.LabelFor(model => model.FirstOrDefault().ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                           @Html.EditorFor(model => model.FirstOrDefault().ProductName, new { htmlAttributes = new { @class = "form-control" } })
                </div>

            </div>

           <div class="form-group">
                         @Html.LabelFor(model => model.FirstOrDefault().ProductQty, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                         @Html.EditorFor(model => model.FirstOrDefault().ProductQty, new { htmlAttributes = new { @class = "form-control" }})
                </div>
            </div>
}
  • You cannot use a `foreach` loop to generate form controls for a collection. Use a `for` loop or a custom `EditorTemplate` –  Feb 14 '16 at 21:34
  • Replace `model.FirstOrDefault()`with just `item` – CodeNotFound Feb 14 '16 at 21:34
  • How would i implement the for loop ? and i never used an EditorTemplate , this is my first time using mvc – Alessandro Feb 14 '16 at 21:35
  • @CodeNotFound, That will not work. The values will not post back and bind to the model. You MUST use a `for` loop of custom `EditorTemplate` –  Feb 14 '16 at 21:39
  • Ha indeed @StephenMuecke you are right. Hope he will go to check the link => http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable – CodeNotFound Feb 14 '16 at 21:40
  • CodeNotFound i get this error now, but i have a paramter of type int ProductQty in the controller The parameters dictionary contains a null entry for parameter 'ProductQty' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Update(System.Guid, System.String, Int32)' – Alessandro Feb 14 '16 at 21:41
  • @Alessandro, That is an entirely different issue and has no relationship to the code you have shown. It self explanatory (you have not provided values for the 2nd method), but if you can work it out then ask a new question. –  Feb 14 '16 at 21:44
  • @StephenMuecke i checked that link, in order to use the for in my case how would i implement that ? – Alessandro Feb 14 '16 at 21:45
  • As per the link! - `for(int i = 0; i < Model.Count; i++) { @Html.EditorFor(m => m[i].ProductName) ..... }` –  Feb 14 '16 at 21:47
  • @StephenMuecke i get a red line under Model.Count and also on m => m[i].ProductName) – Alessandro Feb 14 '16 at 21:51
  • Then make the model `List` instead of `IEnumerable` or use an `EditorTemplate` as per the link - Please read the dupe carefully. –  Feb 14 '16 at 21:54
  • @StephenMuecke i never did such thing of the editortemplate and i am not understanding what the person is saying in the link – Alessandro Feb 14 '16 at 22:00
  • That person is me :) And in your case your need to create an `EditorTemplate` named `/View/Shared/EditorTemplates/OrdersModel.cshtml. where the model is `@model OrdersModel` and in the main view its `@Html.EditorFor(m => m)` –  Feb 14 '16 at 22:09
  • i don't have editortemplate file ? should i create it ? – Alessandro Feb 14 '16 at 22:12
  • this is too difficult to understand is there any other way ? – Alessandro Feb 14 '16 at 22:13
  • @StephenMuecke if i try to change the iqueryable to IList it gives me an error under the select command – Alessandro Feb 14 '16 at 22:15
  • It says iqueryable cannot be converted to List – Alessandro Feb 14 '16 at 22:20
  • Oh dear - just add `.ToList()` after your query - `(from p( .... select new CommonLayer.Models.OrdersModel(){ .... }).ToList();` –  Feb 14 '16 at 22:24
  • that works, but the quantity won't get posted now on submit for multiple products not even for one product where before if i had one product in order it would be changed. – Alessandro Feb 14 '16 at 22:34
  • I have no idea what you mean by you last comment. If you have another issue, ask a new question and show the relevant code –  Feb 14 '16 at 22:37

0 Answers0