1

我有一个具有 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:

产品输入模型

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 中的数据没有传递给 ProductInputModel。我应该如何通过 ViewModels 获得选定的产品?

4

1 回答 1

1

如果要将视图模型用作操作参数,则视图模型必须具有默认的无参数构造函数。否则默认模型绑定器将无法实例化它。您应该确保您的ProductInputModel.

于 2013-03-07T12:01:18.017 回答