view正在将List的Null值返回给mvc4中的控制器 [英] view is returning Null value for List to the controller in mvc4

查看:58
本文介绍了view正在将List的Null值返回给mvc4中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在帖子中,我的视图模型返回List OrderDetail ProductName,Price,Quantity的NUll值,但是对于customerID和NetPrice正在返回视图中的值..

在我的View CustomerOrder和OrderDetail中.DirectDetail给我们一个客户详细信息列表。



公共类CustomerOrder

{

public int CustomerId {get ;组; } $ / $
public int NetPrice {get;组; }

public List< orderdetail>订单清单{get;组; }

public CustomerOrder()

{

Orderlist = new List< orderdetail>();

} < br $>
}

公共类OrderDetail

{

public string ProductName {get;组; }

public int Quantity {get;组; }

public int Price {get;组; }

public int TotalPrice {get {return Price * Quantity; } $

}



这是我的观点,我使用foreach作为列表



@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( Model.Orderlist中的var extra)

{



@ 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 =submitvalue =提交/>





}

@ {

if(ViewData [result]!=&& ViewData [result]!= null)

{

< script type =text / javascriptlang =javascript>

alert(数据已成功保存);

< / script>

}

}



这是我的控制器。在httppost中,在订单列表中返回计数0



公共类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

文件连接到数据库以在Product Name中插入值显示Null Value作为回报



公共类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; < br $>
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),连接))

{

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();

} < br $>
}

}

返回result2;

}}







如何从两个模型的视图返回值我创建,有没有其他方法来获取列表的值f视图

解决方案

您在代码IMO中遇到了一些结构问题,但是这个问题是由以下原因导致的:



 @ foreach( var 额外的 模型.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)
}





你的lambda表达式和迭代器是错误的,所以orderlist是空的。试试这个:

 @ 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 = > 型号。 OrderList [i] .TotalPrice)
@ Html.TextBoxFor(model = > model.OrderList [i] .TotalPrice)
}


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

解决方案

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

@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:

@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)
} 


这篇关于view正在将List的Null值返回给mvc4中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆