Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In post my view model is returning NUll value for List OrderDetail ProductName,Price,Quantity but for customerID and NetPrice are returning the value from view..
In my View CustomerOrder and OrderDetail.The OrderDetail is given us a list in Customer Detail.

public class CustomerOrder
{
public int CustomerId { get; set; }
public int NetPrice { get; set; }
public List<orderdetail> Orderlist { get; set; }
public CustomerOrder()
{
Orderlist = new List<orderdetail>();
}
}
public class OrderDetail
{
public string ProductName { get; set; }
public int Quantity { get; set; }
public int Price { get; set; }
public int TotalPrice { get { return Price * Quantity; } }
}

This is my View , I have used foreach for the List

@model MvcCustomerOrderClass4g.Models.CustomerOrder
@{
ViewBag.Title = "CustomerOrder";
}

CustomerOrder


@using (Html.BeginForm(FormMethod.Post))
{

CustomerOrder

@Html.LabelFor(model => model.CustomerId)


@Html.EditorFor(model => model.CustomerId)
@Html.ValidationMessageFor(model => model.CustomerId)


@Html.LabelFor(model => model.NetPrice)


@Html.EditorFor(model => model.NetPrice)

@foreach(var extra in Model.Orderlist)
{

@Html.LabelFor(model =>extra.ProductName)


@Html.TextBoxFor(model=>extra.ProductName)


@Html.LabelFor(model => extra.Quantity)


@Html.TextBoxFor(model=>extra.Quantity)


@Html.LabelFor(model => extra.TotalPrice)


@Html.TextBoxFor(model=>extra.TotalPrice)

}


<input type="submit" value="Submit" />



}
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
<script type="text/javascript" lang="javascript">
alert("Data saved Successfully");
</script>
}
}

This is my Controller. In httppost is returning count 0 in orderlist

public class CustomerOrderController : Controller
{


public ActionResult CustomerOrder()
{
CustomerOrder model = new Models.CustomerOrder();

OrderDetail dr = new Models.OrderDetail();
dr.ProductName = "1";
dr.Quantity = 1;
dr.Price = 1;
model.Orderlist.Add(dr);
return View(model);

}
[HttpPost]
public ActionResult CustomerOrder(CustomerOrder SelectedOrder)
{
DataBase dataBase = new DataBase();
var result = dataBase.InsertData(SelectedOrder);
ViewData["result"] = result;
return View();
}

}

DataBase.cs
File is connected to the database to insert value in the Product Name is showing Null Value in return

public class DataBase
{
int OrderID = 0;
int result2 = 0;
int result = 0;
public int InsertData(CustomerOrder SelectOrder)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("INSERT INTO Orders(CustomerID,TotalPrice) VALUES(@CustomerID,@TotalPrice)" + " SELECT SCOPE_IDENTITY()", connection))
{
command.Parameters.AddWithValue("@CustomerID", SelectOrder.CustomerId);
command.Parameters.AddWithValue("@TotalPrice", SelectOrder.NetPrice);
connection.Open();
command.CommandType = CommandType.Text;
int OrderID = Convert.ToInt32(command.ExecuteScalar());
result = command.ExecuteNonQuery();

// List<orderdetail> OrderList = new List<orderdetail>();
OrderDetail customerSelectedOrder = new OrderDetail();

var ProductName = customerSelectedOrder.ProductName;
var Price = customerSelectedOrder.Price;
var Quantity = customerSelectedOrder.Quantity;
var totalPrice = customerSelectedOrder.TotalPrice;
using (SqlCommand Command = new SqlCommand("INSERT INTO OrderDetails(OrderID,ProductName,Price,Quantity,TotalPrice) VALUES('" + OrderID + "',@ProductName,@Quantity,@Price,@TotalPrice)", connection))
{
Command.Parameters.AddWithValue("@ProductName", ProductName);
Command.Parameters.AddWithValue("@Quantity", Price);
Command.Parameters.AddWithValue("@Price", Quantity);
Command.Parameters.AddWithValue("@TotalPrice", totalPrice);
Command.CommandType = CommandType.Text;
result2 = Command.ExecuteNonQuery();
}
}
}
return result2;
}}



How to return value from view from two model I Created, Is there any other way to get values for the list from the view
Posted
Updated 25-Nov-14 1:15am
v3

1 solution

You have a couple structure issues in the code IMO, but this issue is being cause by the following:

C#
@foreach(var extra in Model.Orderlist)
{

@Html.LabelFor(model =>extra.ProductName)
@Html.TextBoxFor(model=>extra.ProductName)
@Html.LabelFor(model => extra.Quantity)
@Html.TextBoxFor(model=>extra.Quantity)
@Html.LabelFor(model => extra.TotalPrice)
@Html.TextBoxFor(model=>extra.TotalPrice)
} 


You lambda expressions and iterator are wrong, so the orderlist is empty. Try this:
C#
@for(var i = 0; i < Model.OrderList.Count(); i++)
{

@Html.LabelFor(model =>model.OrderList[i].ProductName)
@Html.TextBoxFor(model=> model.OrderList[i].ProductName)
@Html.LabelFor(model => model.OrderList[i].Quantity)
@Html.TextBoxFor(model=> model.OrderList[i].Quantity)
@Html.LabelFor(model => model.OrderList[i].TotalPrice)
@Html.TextBoxFor(model=> model.OrderList[i].TotalPrice)
} 
 
Share this answer
 
Comments
Member 11166907 25-Nov-14 9:34am    
It is working for @for. How to use it in @foreach
Nathan Minier 25-Nov-14 9:38am    
By writing your own editor template, or compartmentalizing your view models. This is definitely the "Quick answer" fix, though. There is absolutely no reason to use foreach over for when for is the right answer :)
Member 11166907 25-Nov-14 9:45am    
Mmmm... thankyou for the fix

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900