【发布时间】:2016-02-04 13:07:40
【问题描述】:
我有以下控制器方法来获取作为参数的数据IEnumerable<samplemodel> 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<BrochureTemplateProperties>()。 -
@StephenMuecke 正如你所说,这个糟糕的网址对我来说成了一个问题
标签: c# asp.net-mvc linq razor asp.net-mvc-5