Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 3 发布的ViewModel返回空属性_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 3 发布的ViewModel返回空属性

Asp.net mvc 3 发布的ViewModel返回空属性,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我遇到了一个视图模型的问题,它在发布后不断返回null属性。下面是我的代码(可能是一个语法问题或两个调用类或属性的问题,与我在其他文章中看到的名称相同,但我在代码中没有看到任何此类问题): 视图模型: public class ProductItem { public int ProductID { get; set; } public string Code { get; set; } public string Name { get;

我遇到了一个视图模型的问题,它在发布后不断返回null属性。下面是我的代码(可能是一个语法问题或两个调用类或属性的问题,与我在其他文章中看到的名称相同,但我在代码中没有看到任何此类问题):

视图模型:

public class ProductItem
    {
        public int ProductID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string LongDescription { get; set; }
        public int SupplierID { get; set; }
        public string Dimensions { get; set; }
        public double Price { get; set; }
        public bool On_Sale { get; set; }
        public double DiscountedPrice { get; set; }
        public string Thumbnail { get; set; }
        public string LargeImage { get; set; }
        public string LargeImage2 { get; set; }
        public string LargeImage3 { get; set; }
        public string CrossRef { get; set; }
        public byte Available { get; set; }
        public double Weight { get; set; }
        public byte Important { get; set; }
        public virtual ICollection<ProductCategory> ProductCategories { get; set; }

        // this is required on the page to allow products to be marked for deletion
        public bool IsForDelete { get; set; }
    }

    public class ProductListViewModel
    {
        public IEnumerable<ProductItem> ProductItems { get; set; }
        public IEnumerable<Category> CategoryItems { get; set; }
    }
视图:

@model Admin.Models.ViewModels.ProductListViewModel
@{
ViewBag.Title=“查看产品”;
}
@使用(Html.BeginForm())
{
截至@DateTime.Now.ToString(“dd/MM/yyyy”)的产品列表
排序方式:
产品ID
代码
名称
价格
提升
下降的
仅显示:(类别)
@Html.DropDownList(“CategoryID”,新选择列表(Model.CategoryItems,“CategoryID”,“Name”),“所有类别”)

产品总数:@Model.ProductItems.Count() 编辑 代码 名称 价格 打折 折扣价格 重量 删除 @对于(var i=0;im.ProductItems.ToList()[i].ProductID) @ActionLink(Model.ProductItems.ToList()[i].ProductID.ToString(),“ProductEdit”,新的{id=Model.ProductItems.ToList()[i].ProductID}) @DisplayFor(m=>m.ProductItems.ToList()[i].Code) @DisplayFor(m=>m.ProductItems.ToList()[i].Name) @Html.EditorFor(m=>m.ProductItems.ToList()[i].Price) @Html.CheckBoxFor(m=>m.ProductItems.ToList()[i].On_Sale,新的{id=“On_Sale”+Model.ProductItems.ToList()[i].ProductID}) @(m=>m.ProductItems.ToList()[i].DiscountedPrice) @Html.EditorFor(m=>m.ProductItems.ToList()[i].Weight) @(m=>m.ProductItems.ToList()[i].IsForDelete,新的{id=Model.ProductItems.ToList()[i].ProductID}) @ActionLink(“编辑”,“产品编辑”,新的{id=Model.ProductItems.ToList()[i].ProductID})| @ActionLink(“详细信息”,“详细信息”,新的{id=Model.ProductItems.ToList()[i].ProductID})| @ActionLink(“删除”,“删除”,新的{id=Model.ProductItems.ToList()[i].ProductID}) }

@ActionLink(“添加新产品”、“产品添加”)

}

在post操作中,productViewModel参数的ProductItems和CategoryItems属性为null。

确定,因此存在两个问题

  • 我不明白你为什么要发布
    类别项目列表
    你应该只看到所选类别,而不是列表

  • ProductItems
    的问题是为
    标记生成的名称。当前,正在生成的名称是
    name=“[0]。Price”
    ,而它应该是
    name=“ProductItems[0]。Price”

  • 我更改了以下代码

    @Html.EditorFor(m => m.ProductItems.ToList()[i].Price)
    

    它成功了


    注意:我在
    ProductListViewModel
    中将
    IEnumerable ProductItems
    更改为
    List ProductItems
    ,因此存在两个问题

  • 我不明白你为什么要发布
    类别项目列表
    你应该只看到所选类别,而不是列表

  • ProductItems
    的问题是为
    标记生成的名称。当前,正在生成的名称是
    name=“[0]。Price”
    ,而它应该是
    name=“ProductItems[0]。Price”

  • 我更改了以下代码

    @Html.EditorFor(m => m.ProductItems.ToList()[i].Price)
    

    它成功了


    注意:我将
    ProductListViewModel

    中的
    IEnumerable ProductItems
    更改为
    List ProductItems
    ,返回时它将为空。在我的一个项目中,我有一个类似的表格,考虑到我的情况,我会这样做

    您可以将视图模型更改为如下所示,这样就不必在视图中进行太多的列表转换:

    public class ProductListViewModel
    {
         public List<ProductItem> ProductItems { get; set; }
    
         public List<Category> CategoryItems { get; set; }
    }
    
    公共类ProductListViewModel
    {
    公共列表ProductItems{get;set;}
    公共列表类别项{get;set;}
    }
    
    现在在您的视图中,它可能看起来像这样(这只是它的一部分,其余部分您可以继续添加):

    @for(int i=0;im.ProductItems[i].Name)
    @Html.HiddenFor(m=>m.ProductItems[i].Name)
    @Html.CheckBoxFor(m=>m.ProductItems[i].isfordelite)
    }
    
    添加代码并进行一些调试,以查看提交时如何返回值


    我希望这能有所帮助。

    是的,在回邮时它将为空。在我的一个项目中,我有一个类似的表格,考虑到我的情况,我会这样做

    您可以将视图模型更改为如下所示,这样就不必在视图中进行太多的列表转换:

    public class ProductListViewModel
    {
         public List<ProductItem> ProductItems { get; set; }
    
         public List<Category> CategoryItems { get; set; }
    }
    
    公共类ProductListViewModel
    {
    公共列表ProductItems{get;set;}
    公共列表类别项{get;set;}
    }
    
    现在在您的视图中,它可能看起来像这样(这只是它的一部分,其余部分您可以继续添加):

    @for(int i=0;im.ProductItems[i].Name)
    @Html.HiddenFor(m=>m.ProductItems[i].Name)
    @Html.Ch
    
    public class ProductListViewModel
    {
         public List<ProductItem> ProductItems { get; set; }
    
         public List<Category> CategoryItems { get; set; }
    }
    
    @for (int i = 0; i < Model.ProductItems.Count(); i++)
    {
         <tr>
              <td>
                   @Html.DisplayFor(m => m.ProductItems[i].Name)
                   @Html.HiddenFor(m => m.ProductItems[i].Name)
              </td>
         </tr>
         <tr>
              <td>
                   @Html.CheckBoxFor(m => m.ProductItems[i].IsForDelete)
              </td>
         </tr>
    }