I've been googling for 3 days now and I'm totally lost between different methods of doing what I want. I want to add a DropDownList to my view for a foreign key.
These are my model classes:
public class PaymentType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}
public class Payment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public PaymentType PaymentType { get; set; }
public int Amount { get; set; }
public string Date { get; set; }
}
and this is my controller:
public ActionResult Payment()
{
return View();
}
Then I add a view for Create
from model:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Payment</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Amount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Amount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
I tried to add the dropdownlist Like this:
<div class="form-group">
@Html.LabelFor(model => model.PaymentType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PaymentType, new SelectList(Model.PaymentType, "Id", "Name", Model.PaymentTypet), "--select--")
@Html.ValidationMessageFor(model => model.PaymentType, "", new { @class = "text-danger" })
</div>
But I get this error:
Error CS1503 Argument 1: cannot convert from 'Charity.Models.PaymentType' to 'System.Collections.IEnumerable'
I don't know if the problem is from the model, controller, view or all of them.
Any suggestion would be appreciated.