Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 视图模型IEnumerable<&燃气轮机;属性从post方法返回null(非绑定)?_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 视图模型IEnumerable<&燃气轮机;属性从post方法返回null(非绑定)?

Asp.net mvc 视图模型IEnumerable<&燃气轮机;属性从post方法返回null(非绑定)?,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个视图模型,它包含一个产品类类型和一个IEnumerable类型。在post上,第一级产品对象从viewmodel绑定回来,而产品枚举返回null 为什么IEnumerable属性没有按照帖子绑定到我的视图模型?谢谢 型号: public class Product { public int ID { get; set; } public string Name { get; set; } public decimal Price { get; set; } }

我有一个视图模型,它包含一个产品类类型和一个IEnumerable类型。在post上,第一级产品对象从viewmodel绑定回来,而产品枚举返回null

为什么IEnumerable属性没有按照帖子绑定到我的视图模型?谢谢

型号:

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ProductIndexViewModel
{
    public Product NewProduct { get; set; }
    public IEnumerable<Product> Products { get; set; }
}

public class BoringStoreContext 
{
        public BoringStoreContext()
         {
             Products = new List<Product>();
             Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
             Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
         }
         public List<Product> Products {get; set;}
}

您没有正确使用lambda表达式。您需要通过模型访问产品列表。试着这样做:

@count = 0
foreach (var item in Model.Products)
    { 
     <div>
    @Html.LabelFor(model => model.Products[count].ID)
    @Html.EditorFor(model => model.Products[count].ID)
</div>
<div>
    @Html.LabelFor(model => model.Products[count].Name)
    @Html.EditorFor(model => model.Products[count].Name)
</div>
<div>
    @Html.LabelFor(model => model.Products[count].Price)
    @Html.EditorFor(model => model.Products[count].Price)
</div>
@count++
    }
模型

公共类产品
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共十进制价格{get;set;}
}
公共类ProductIndexViewModel
{
公共产品新产品{get;set;}
公共列表产品{get;set;}
}
公共类BoringStoreContext
{
公共BoringStoreContext()
{
产品=新列表();
Products.Add(newproduct(){ID=1,Name=“Sure”,Price=(十进制)(1.10)});
添加(新产品(){ID=2,Name=“Sure2”,价格=(十进制)(2.10)});
}
公共列表产品{get;set;}
}
视图:

@model Models.ProductIndexViewModel
@使用(@Html.BeginForm())
{
@LabelFor(model=>model.NewProduct.Name)
@EditorFor(model=>model.NewProduct.Name)
@LabelFor(model=>model.NewProduct.Price)
@EditorFor(model=>model.NewProduct.Price)
对于(int count=0;countmodel.Products[count].ID)
@Html.EditorFor(model=>model.Products[count].ID)
@Html.LabelFor(model=>model.Products[count].Name)
@Html.EditorFor(model=>model.Products[count].Name)
@Html.LabelFor(model=>model.Products[count].Price)
@Html.EditorFor(model=>model.Products[count].Price)
}
}
将解决这个问题。下面是使用EditorTemplates的另一种方法

您可以使用
编辑器模板
来显示产品,它可以正常工作

1)在视图/主文件夹下创建一个名为“EditorTemplates”的文件夹

2)在其中添加一个名为“
Products
”的视图。 将此代码添加到该视图

@model SO_MVC.Models.Product
@{
   Layout = null;
}
<p>
  @Html.LabelFor(x=>x.ID)
  @Html.EditorFor(x=>x.ID) 
</p>
<p>
  @Html.LabelFor(x=>x.Name)
  @Html.EditorFor(x=>x.Name)
</p>
<p>
  @Html.LabelFor(x=>x.Price)
  @Html.EditorFor(x=>x.Price)
</p> 
@Html.HiddenFor(x=>x.Id)

你定制了你的EditorFor吗?@Shyju:嗨,我没有。我假设如果我为Product.cshtml创建一个EditorTemplate,我可以调用@HTml.EditorFor(model=>model.Products),它可能会工作?是的,你可以。我用这种方法补充了一个答案。我也有同样的问题。但是仍然找不到解决方案为什么要使用foreach循环来模拟常规for循环?为什么在foreach循环中已经有了该项目时,还要反复调用model.Products[count]?@Xander-谢谢。助手对使用
扩展没有很好的响应,因此
model.Products.item.ID
不起作用,因此索引。但是,我忘了从解决方案中删除
.item
,并对其进行了编辑,以反映您在第一条评论中所评论的更改。@rossisdead-如上所述,
model.Products.item
在此处的有效性不如使用时好。(int count=0;count也就足够了。啊,我去测试了。我没有意识到使用索引器会导致呈现名称属性也包含索引。干净利落@JaJ-在lambda表达式中,当您从
model=>
开始时,您必须在下一部分中访问它。MSDN:“lambda表达式x=>x*x被读取为“x到x乘以x”。因此,如果需要绑定,则需要在表达式的后半部分使用该模型。至于使用带有点符号的
foreach
进行访问,在我看来,这是一个缺失的特性,急需MVC/Razor团队实现。
@count = 0
foreach (var item in Model.Products)
    { 
     <div>
    @Html.LabelFor(model => model.Products[count].ID)
    @Html.EditorFor(model => model.Products[count].ID)
</div>
<div>
    @Html.LabelFor(model => model.Products[count].Name)
    @Html.EditorFor(model => model.Products[count].Name)
</div>
<div>
    @Html.LabelFor(model => model.Products[count].Price)
    @Html.EditorFor(model => model.Products[count].Price)
</div>
@count++
    }
BoringStoreContext db = new BoringStoreContext();

    public ActionResult Index()
    {
        ProductIndexViewModel viewModel = new ProductIndexViewModel
        {
            NewProduct = new Product(),
            Products = db.Products
        };
        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Index(ProductIndexViewModel viewModel)
    {
        // work with view model

        return View();
    }
public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ProductIndexViewModel
{
    public Product NewProduct { get; set; }
    public List<Product> Products { get; set; }
}

public class BoringStoreContext
{
    public BoringStoreContext()
    {
        Products = new List<Product>();
        Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
        Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
    }
    public List<Product> Products { get; set; }
}
@model Models.ProductIndexViewModel


@using (@Html.BeginForm())
{
<div>
    @Html.LabelFor(model => model.NewProduct.Name)
    @Html.EditorFor(model => model.NewProduct.Name)
</div>
<div>
    @Html.LabelFor(model => model.NewProduct.Price)
    @Html.EditorFor(model => model.NewProduct.Price)
</div>


for (int count = 0; count < Model.Products.Count; count++ )
{ 
    <div>
    @Html.LabelFor(model => model.Products[count].ID)
    @Html.EditorFor(model => model.Products[count].ID)
    </div>
    <div>
    @Html.LabelFor(model => model.Products[count].Name)
    @Html.EditorFor(model => model.Products[count].Name)
    </div>
    <div>
    @Html.LabelFor(model => model.Products[count].Price)
    @Html.EditorFor(model => model.Products[count].Price)
    </div>
}

<div>
    <input type="submit" value="Add Product" />
</div>
}
@model SO_MVC.Models.Product
@{
   Layout = null;
}
<p>
  @Html.LabelFor(x=>x.ID)
  @Html.EditorFor(x=>x.ID) 
</p>
<p>
  @Html.LabelFor(x=>x.Name)
  @Html.EditorFor(x=>x.Name)
</p>
<p>
  @Html.LabelFor(x=>x.Price)
  @Html.EditorFor(x=>x.Price)
</p> 
@Html.HiddenFor(x=>x.Id)
  @Html.EditorFor(m=>m.Products)