I am trying to assign int value present in view-bag to drop-down list.
View
@Html.DropDownListFor(m => m.nStatusID, ViewBag.ReasonTypeList asSelectList,ViewBag.nStatusID, new { @class = "form-control" })
controller action contain-
var Complaint = db.ComplaintRegistrations.SingleOrDefault(x => x.nCallID == id);
ViewBag.nStatusID = Complaint.nStatusID;
List<ReasonTypeMaster> ReasonTypeList = db.ReasonType.ToList();
ViewBag.ReasonTypeList = new SelectList(ReasonTypeList, "nReasonTypeID", "cReasonType");
Error- 'System.Web.Mvc.HtmlHelper' has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
It looks like you want to set the selected value of the dropdown list for that you need to set it in the model object and the helper will take care of setting as selected. Try like:
var Complaint = db.ComplaintRegistrations.SingleOrDefault(x => x.nCallID == id);
if(Complaint != null)
model.nStatusID = Complaint.nStatusID;
List<ReasonTypeMaster> ReasonTypeList = db.ReasonType.ToList();
ViewBag.ReasonTypeList = new SelectList(ReasonTypeList, "nReasonTypeID", "cReasonType");
return View(model);
and in View you only will need:
@Html.DropDownListFor(m => m.nStatusID,
ViewBag.ReasonTypeList asSelectList,
new { @class = "form-control" })
The m.nStatusID
will have the value that you set in the controller action and will be populated as selected in the drop down element.