Reputation: 1368
I want to send a List<Tupple<>>
to a controller but I'm not doing it well because I always get the message No parameterless constructor defined for this object
and the method's code doesn't get executed.
I'm using a class as a view-model for the controller arguments:
public class InsurancesHospitals
{
public int hospital { get; set; }
public List<Tuple<int, string, bool>> insurances { get; set; }
}
This is the controller:
[Authorize]
public ActionResult AssociateInsurances(InsurancesHospitals viewModel)
{
foreach (var insurance in viewModel.insurances)
{
/*
Here I do some things...
*/
}
return Redirect("/Hospitals/Index");
}
And this is the view from where I call the controller:
<form id="formulario" role="form" method="post" action="~/Hospitals/AssociateInsurances">
<label class="control-label" for="hospital">Hospital</label>
<select class="form-control" id="hospital" name="hospital">
@foreach (var hospital in (IQueryable<HOSPITALS>)ViewData["hospitals"])
{
<option value="@hospital.ID" >@hospital.NAME</option>
}
</select>
<div class="col-xs-12">
<div class="row">
@{
var insurances = ((IQueryable<INSURANCES>)ViewData["insurances"]).ToList();
for (var index=0; index < insurances.Count(); index++)
{
<input type="hidden" value="@insurances[index].ID" class="insurances" name="insurances[@index].Item1" />
<div class="col-xs-3">
<input type="checkbox" data-id="@insurances[index].ID" class="association" name="insurances[@index].Item3" />
@insurances[index].NAME
</div>
<div class="col-xs-3">
<input type="text" data-id="@insurances[index].ID" class="form-control codes" name="insurances[@index].Item2" />
</div>
}
}
</div>
</div>
<button class="btn btn-success" type="submit">
<span>Associate</span>
</button>
</form>
Upvotes: 2
Views: 3529
Reputation: 151604
Like the error says: a tuple has no parameterless constructor, so the model binder can't instantiate it.
You can go the hard way: create your own model binder, or the easy way: simply introduce a new class that holds the appropriate properties. This has an added bonus: actually useful property names as opposed to ItemN
.
Upvotes: 3