2015-11-04 62 views
0

我有如下控制器的方法来获得数据,作为参数作为IEnumerable<samplemodel> model“的IEnumerable <samplemodel>模式”值填入到另一个模型

[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; } 

} 

到此控制器,数据像下面传递

图片1:10个元素[0-9索引]

enter image description here

图片二:

enter image description here

我想选择有限元素的属性这是本0-9索引的元素IsChecked财产True,并返回

第0个索引元素的属性

分配该限制元素的属性我创建了另一个模型类如下

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; } 
} 

于是我尝试做一些像下面

方法1:

[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解决方案来解决这个

编辑

,几乎所有的编译错误走了,然后,我创建详细查看,但一旦我运行此得到一个错误页面这样

enter image description here

+0

尝试用'你的LINQ查询.ToList ()'后。 –

+0

@StephenMuecke你说这个可怕的网址对我来说成了一个问题 – kez

回答

4

您的方法1是正确的,因为您正在投影anonymous类型并将其存储在IEnumerable<BrochureTemplateProperties>中,所以出现错误。简单地投射型BrochureTemplateProperties这样的: -

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

更新:

好了,你的看法是预计Models.BrochureTemplateProperties但你逝去的IEnumerable<BrochureTemplateProperties>所以你需要获取第一个匹配的对象。只要在最后添加FirstOrDefault: -

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

这可能没问题试试 – kez

+1

@kez,虽然Rahul Singh是正确的,但你的方法不是。您绝不应该有一个参数,它是GET方法中复杂对象的集合。除了你生成的糟糕的url之外,你可以很容易地超出查询字符串限制并抛出一个异常。 –

+0

一旦我使用你的解决方案几乎所有的编译错误消失了,然后我创建了详细信息视图,但一旦我运行这个有这样的错误页面 http://s16.postimg.org/gvpldz61h/errorz.jpg – kez

0

尝试用这个 -

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

    }).ToList<BrochureTemplateProperties>(); 
+0

得到这种补全时间错误IEnumerable <<匿名类型:布尔IsChecked,字符串名称,字符串PropertyValue >>'不包含'ToList'的定义和最佳扩展方法重载'Enumerable.ToList (IEnumerable )''需要类型为'IEnumerable '的接收器' – kez

相关问题