【问题标题】:"IEnumerable<samplemodel> model" values fill to another model“IEnumerable<samplemodel> model”值填充到另一个模型
【发布时间】:2016-02-04 13:07:40
【问题描述】:

我有以下控制器方法来获取作为参数的数据IEnumerable&lt;samplemodel&gt; model

    [HttpGet]
    public ActionResult Create_Template(IEnumerable<ProductsPropertiesVM> model)
    {
        return View(...);
    }

这是ProductsPropertiesVM 模型类

public class ProductsPropertiesVM
{


    public int Property_ID { get; set; }
    public string Property_Title { get; set; }
    public string Property_Value { get; set; }
    public bool IsChecked { get; set; }

    public string Product_Name { get; set; }       
    public string Product_Description { get; set; }       
    public string Prodcut_Features { get; set; }
    public string Unique_Selling_Propositions { get; set; }        
    public string Business_Case_Feasibity_Study { get; set; }
    public string Sharia_Resolution_and_Requirement_for_Product { get; set; }
    public string Approved_Accounting_Entries { get; set; }
    public string Listing_of_Risk_Related_Procedures { get; set; }
    public string Legal_Requirement { get; set; }
    public string Listing_of_Internal_Procedures_for_Review { get; set; }
    public string Product_Statistics_Targeted_Segment { get; set; }
    public string Product_Statistics_Sales_Volume { get; set; }
    public string Product_Statistics_Profitability { get; set; }
    public string Product_Statistics_Annual_Growth_Rate { get; set; }
    public string Relevent_Case_Studies { get; set; }

}

到这个控制器,数据像下图一样传递

图像一:10 个元素 [0-9 个索引]

图二:第0个索引元素的属性

我想选择有限元素的属性,即这0-9个索引元素的IsChecked属性True,并返回它

为了分配有限元素的属性,我创建了另一个模型类,如下所示

public class TemplateProperties
{
    public int Property_ID { get; set; }
    public string Property_Title { get; set; }
    public string Property_Value { get; set; }
    public bool IsChecked { get; set; }
}

然后我尝试做类似下面的事情

方法一:

    [HttpGet]
    public ActionResult Create_Template(IEnumerable<ProductsPropertiesVM> model)
    {

        IEnumerable<BrochureTemplateProperties> sample = model.Where(y => y.IsChecked).Select(y => new
        {
            sample.IsChecked = y.IsChecked,
            sample.Name = y.Property_Title,
            sample.PropertyValue = y.Property_Value

        });

        return View(sample);
    };

但这会出现编译错误

方法2:

    [HttpGet]
    public ActionResult Create_Template(IEnumerable<ProductsPropertiesVM> model)
    {

        var sample = model.Where(y => y.IsChecked).Select(y => new
        {
            IsChecked = y.IsChecked,
            Name = y.Property_Title,
            PropertyValue = y.Property_Value

        });

        return View(sample);
    };

但是这种方法一旦我去查看模型值就不会绑定到 TemplateProperties 模型,

我能做些什么来纠正这个问题

编辑

一旦我使用@Rahuls 解决方案,几乎所有编译错误都消失了,然后我创建了详细信息视图,但是一旦我运行它就会得到一个像这样的错误页面

【问题讨论】:

  • 在您的 linq 查询之后尝试使用 .ToList&lt;BrochureTemplateProperties&gt;()
  • @StephenMuecke 正如你所说,这个糟糕的网址对我来说成了一个问题

标签: c# asp.net-mvc linq razor asp.net-mvc-5


【解决方案1】:

您的方法 1 是正确的,因为您正在投影 anonymous 类型并将其存储在 IEnumerable&lt;BrochureTemplateProperties&gt; 中,所以您遇到了错误。只需像这样投影类型BrochureTemplateProperties:-

 IEnumerable<BrochureTemplateProperties> sample = model.Where(y => y.IsChecked)
        .Select(y => new BrochureTemplateProperties
        {
            IsChecked = y.IsChecked,
            Name = y.Property_Title,
            PropertyValue = y.Property_Value
        });

更新:

好的,所以您的 View 期待 Models.BrochureTemplateProperties,但您正在传递 IEnumerable&lt;BrochureTemplateProperties&gt;,因此您需要获取第一个匹配的对象。只需在末尾添加FirstOrDefault:-

 .Select(y => new BrochureTemplateProperties
            {
                IsChecked = y.IsChecked,
                Name = y.Property_Title,
                PropertyValue = y.Property_Value
            }).FirstOrDefault(); //here

【讨论】:

  • 这个应该没问题 我试试
  • @kez,虽然 Rahul Singh 是正确的,但您的方法却不是。您永远不应该在 GET 方法中有一个参数是复杂对象的集合。除了生成的 URL 很糟糕之外,您还可以轻松超出查询字符串限制并引发异常。
  • 一旦我使用了你的解决方案,几乎所有的编译错误都消失了,然后我创建了详细信息视图,但是一旦我运行它就会得到一个像这样的错误页面s16.postimg.org/gvpldz61h/errorz.jpg
  • @kez - 我看不到图片。斯蒂芬也是绝对正确的,我没有注意到你在 GET 请求中这样做,如果可能的话尝试改变你的方法。
  • @kez - 不,我做不到,他们被阻止了。
【解决方案2】:

试试这个 -

var sample = model.Where(y => y.IsChecked).Select(y => new
    {
        IsChecked = y.IsChecked,
        Name = y.Property_Title,
        PropertyValue = y.Property_Value

    }).ToList<BrochureTemplateProperties>();

【讨论】:

  • 得到这种完整的时间错误'IEnumerable&lt;&lt;anonymous type: bool IsChecked, string Name, string PropertyValue&gt;&gt;' does not contain a definition for 'ToList' and the best extension method overload 'Enumerable.ToList&lt;BrochureTemplateProperties&gt;(IEnumerable&lt;BrochureTemplatePrope‌​rties&gt;)' requires a receiver of type 'IEnumerable&lt;BrochureTemplateProperties&gt;'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多