1
votes

I have a product which has 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; }
}

And for showing a list of products I have another 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; }
}

And in my View I'm using Product LisViewModel to show a list of products. And in every product I want to add radio button (to select that product)

<% = Html.RadioButtonFor(m => m.List[i].Selected, "false", Model.List[i]. ProductID)%>

For getting all the data in my controller I created 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; }
}

In my controller input is empty : [HttpPost] public ActionResult Details(ProductInputModel input) {}

I can't figure out whydata from ProductViewModel not passes to ProductInputModel. How I should get the selected product through ViewModels?

1

1 Answers

1
votes

Your view models must have default parameterless constructors if you want to use them as action parameters. Otherwise the default model binder will not be able to instantiate it. You should make sure that you have a parameterless constructor to your ProductInputModel.