And this is the dropdown menu I used
<select id="IDmoto" name="IDmoto" class="combobox">
<option>@siteResource1.motorisation</option>
</select>
And this is the dropdown menu I used
<select id="IDmoto" name="IDmoto" class="combobox">
<option>@siteResource1.motorisation</option>
</select>
In the image you provided, you either tried to get a Form named "IDmoto" that doesn't exist in the collection or tried to get a control named "IDmoto". The FormCollection is a collection of forms, but you can get the collection of controls using FormCollection["FormName"].Controls, or simply FormName.Controls. Using your code:
public ActionResult Resultat(FormCollection forms)
{
// Just realized that you specified the control as a combobox
// The SelectedIndex is already an int.
int IDMotorisation = forms["FormName"].Controls["IDmoto"].SelectedIndex;
List<Piece> pieces = new List<Piece>();
foreach (Piece item in db.Pieces)
{
if (IDMotorisation == item.IDMotorisation)
{
pieces.Add(item);
}
}
}
or, only calling one form:
public ActionResult Resultat(Form form)
{
int IDMotorisation = form.Controls["IDmoto"].SelectedIndex;
List<Piece> pieces = new List<Piece>();
foreach (Piece item in db.Pieces)
{
if (IDMotorisation == item.IDMotorisation)
{
pieces.Add(item);
}
}
}
The meaning of the NullReferenceException is caused by referencing an object that is null. With the FormCollection, if the FormCollection can't find the form specified, it will return null. More NullReferenceException information here.
Welcome to stackoverflow, by the way!