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