我有一个有viewModel的产品: ProductViewModel
private int _id;
private string _name;
private string _type;
private int _selectedID;
public ProductViewModel(int id, string name, string type)
{
_ id = id;
_ name = name;
_ type = type;
}
public int ProductID
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public int Selected
{
get { return _selected; }
set { _selected = value; }
}
为了显示产品列表,我有另一个ViewModel: ProductListViewModel
private PagedResult<ProductViewModel> _pagedResult;
private string _sortColumn = "Id";
private string _sortOrder = "ASC";
public LostAssetsListViewModel(PagedResult< ProductViewModel > products)
{
_pagedResult = products;
}
public PagedResult<ProductViewModel> List
{
get { return _pagedResult; }
set { _pagedResult = value; }
}
public string SortColumn
{
get { return _sortColumn; }
set { _sortColumn = value; }
}
public string SortOrder
{
get { return _sortOrder; }
set { _sortOrder = value; }
}
在我的视图中,我使用Product LisViewModel来显示产品列表。在每个产品中我都要添加单选按钮(以选择该产品)
<% = Html.RadioButtonFor(m => m.List[i].Selected, "false", Model.List[i]. ProductID)%>
为了获取控制器中的所有数据,我创建了ProductInputModel:
ProductInputModel
private int _id;
private string _name;
private string _type;
private int _selectedID;
public ProductInputModel(int id, string name, string type)
{
_ id = id;
_ name = name;
_ type = type;
}
public int ProductID
{
get { return _id; }
set { _id = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public int Selected
{
get { return _selected; }
set { _selected = value; }
}
在我的控制器输入为空: [HttpPost] public ActionResult Details(ProductInputModel input) {}
我无法弄清楚ProductViewModel中的whydata没有传递给ProductInputModel。我应该如何通过ViewModels获取所选产品?
答案 0 :(得分:1)
如果要将它们用作操作参数,则视图模型必须具有默认的无参数构造函数。否则,默认模型绑定器将无法实例化它。您应该确保ProductInputModel
有一个无参数构造函数。