My code is like this
namespace DiagnosisApp.Models
{
public class ProcedurePrice
{
public int ID { get; set; }
public int DepartmentID { get; set; }
public int ProcedureID { get; set; }
public int InsuranceProviderID { get; set; }
public int ProcedureCategoryID { get; set; }
public int ProcedureSubCategoryID { get; set; }
[ForeignKey("ID")]
public virtual Department Department { get; set; }
[ForeignKey("ID")]
public virtual InsuranceProvider InsuranceProvider { get; set; }
[ForeignKey("ID")]
public virtual ProcedureCategory ProcedureCategory { get; set; }
[ForeignKey("ID")]
public virtual ProcedureSubCategory ProcedureSubCategory { get; set; }
}
}
Controller
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
return View();
}
And in my view
@Html.DropDownList("ProcedureID", null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProcedureID)
Everything looks okay to me, But it throws an error
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProcedureID'.
Can anyone point out what I am doing wrong here?
if you use partial, thats very simple ,you can find solution on error this error say you have not View Data With type SelectListItem that key was 'ProcedureId' i think you use Partial for your create and call it on your Index Page So you should passing this code on your index action result as like as my code:
I suppose you're missing something like
I think you have to inizialize ViewBag.ProcedureId
The mistake is you are putting it in
ViewBag.ProcedureSubCategoryID
while you are passingProcedureID
intHtml.DropDownList()
and also you are passingSelectList
parameternull
. A quick fix is to just replaceProcedureSubCategoryID
withProcedureID
inHtml.DropDownList()
as key in first parameter:you have three ways to resolve this.
Way 1:
Instead of passing
null
in second parameter which accepts of typeSelectList
you can do something like this:Way2:
Way 3:
or alternative is to store it in
ProcedureID
in your action:and in View: