locked
Problem with opening DropDownListFor RRS feed

  • Question

  • User-1842880510 posted

    I am having a problem with this bit of code.

                    <div class="editor-label">
                        @Html.LabelFor(model => model.RelatedFMSData.FMSData.FMSCountyCodeID, "FMS Country Code")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.RelatedFMSData.FMSData.FMSCountyCodeID,  Model.RelatedFMSData.DDCountyCodeList, "Select Country",new { id = "countrySelect" })
                        @Html.ValidationMessageFor(model => model.RelatedFMSData.FMSData.FMSCountyCodeID)
                    </div>

    This code exists on three pages.  Two of them have the exact same model, one had a slightly different model but all of the model are the exactly the same in this area.
    When this code loads the view Create, and Edit.  Everyting loads correctly.  However, when this code loads in EditNew it throws the below error.

    "The ViewData item that has the key 'RelatedFMSData.FMSData.FMSCountyCodeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'."

    I am confused because the Model.RelatedFMSData.DDCountyCodeList is in fact a SelectList object., whereas the 'RelatedFMSData.FMSData.FMSCountyCodeID' will store the value of the dropdown.  Not only does the exact same code work on the other two views, it was also working yesterday afternoon just before leaving work.  Now the first thing this morning it doesn't.  I am not able to understand what it is saying the problem is.

    I could really use some help here.

    Wednesday, May 14, 2014 10:50 AM

Answers

  • User-1842880510 posted

    Resolution of the first Error message: See code below from the controller.

    Original Code:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).First();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName", reqForEdit.RelatedFMSData.FMSData.FMSCountyCodeID);
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Changed Code:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_ext_FMSRequirementData_Get(id).FirstOrDefault();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData.FMSRelatedReqInfoID = FMSData.FMSRelatedReqInfoID;
                        RelatedFMS.FMSData.FMSCountyCodeID = FMSData.FMSCountyCodeID;
                        RelatedFMS.FMSData.FMSCaseNumber = FMSData.FMSCaseNumber;
                        RelatedFMS.FMSManager = FMSData.FMSManager;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Note that SelectList for CountryCodeID is no longer being generated.
    As Corrected:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_ext_FMSRequirementData_Get(id).FirstOrDefault();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData.FMSRelatedReqInfoID = FMSData.FMSRelatedReqInfoID;
                        RelatedFMS.FMSData.FMSCountyCodeID = FMSData.FMSCountyCodeID;
                        RelatedFMS.FMSData.FMSCaseNumber = FMSData.FMSCaseNumber;
                        RelatedFMS.FMSManager = FMSData.FMSManager;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName", reqForEdit.RelatedFMSData.FMSData.FMSCountyCodeID);
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Now the SelectList is beinbg generated and populated.

    For the second error:

    The stored Procedure being run to update the model, calls a second stored procedure, which is erroring out with an inability to convert varchar to int.

    This causes the view to be resubmitted with the model that was submitted to the edit.  However, now it is apparently missing the selectList property for StatusID.  Why this is the case I don't know yet.  I will need to research the code. 

    Update: 

    Turns out all of the DDL SelectList properties of the original model are null when the model is submitted to the Edit Post method.  While this would not be an issue when the Updates are successful, if there is an error and the view needs to be resubmitted, it is missing all of the SelectList properties needed to populate the DropDownListFor's

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 14, 2014 12:55 PM

All replies

  • User-1657171777 posted

    Are you using a ViewBag object in either your action or View?  If you are, you might have conflicting code between a ViewBag object name and another piece of code in your view.

    Wednesday, May 14, 2014 11:09 AM
  • User-1842880510 posted

    Not using a Viewbag except for Viewbag.Title =  "Edit"

    See stack trace below

    [InvalidOperationException: The ViewData item that has the key 'RelatedFMSData.FMSData.FMSCountyCodeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.]
       System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name) +418649
       System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +141
       System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +92
       System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, Object htmlAttributes) +72
       ASP._Page_Views_Requirements_EditNew_cshtml.Execute() in c:\_D\Visual Studio 2012 Projects\VFS_ProcurementTracking\Views\Requirements\EditNew.cshtml:193
    

    I did so other stuff on this page and there is no problem opening showing this select on the "EditNew" view when no value has been assigned to it, the problem is only an issue when the value already assigned.  This does not occur in the old "Edit" view.

    Wednesday, May 14, 2014 11:23 AM
  • User-1842880510 posted

    It is also happening in another dropdownlistfor when the form is submitted.

    [InvalidOperationException: The ViewData item that has the key 'Requirement.StatusID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.]
       System.Web.Mvc.Html.SelectExtensions.GetSelectData(HtmlHelper htmlHelper, String name) +418649
       System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, ModelMetadata metadata, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes) +141
       System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel, IDictionary`2 htmlAttributes) +92
       System.Web.Mvc.Html.SelectExtensions.DropDownListFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IEnumerable`1 selectList, String optionLabel) +61
       ASP._Page_Views_Requirements_EditNew_cshtml.Execute() in c:\_D\Visual Studio 2012 Projects\VFS_ProcurementTracking\Views\Requirements\EditNew.cshtml:151
    

    Wednesday, May 14, 2014 11:31 AM
  • User-1842880510 posted

    Resolution of the first Error message: See code below from the controller.

    Original Code:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).First();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName", reqForEdit.RelatedFMSData.FMSData.FMSCountyCodeID);
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Changed Code:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_ext_FMSRequirementData_Get(id).FirstOrDefault();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData.FMSRelatedReqInfoID = FMSData.FMSRelatedReqInfoID;
                        RelatedFMS.FMSData.FMSCountyCodeID = FMSData.FMSCountyCodeID;
                        RelatedFMS.FMSData.FMSCaseNumber = FMSData.FMSCaseNumber;
                        RelatedFMS.FMSManager = FMSData.FMSManager;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Note that SelectList for CountryCodeID is no longer being generated.
    As Corrected:

                    if (db.prc_FMS_Requirement_Data.Where(F => F.ReqID == id).Count() > 0)
                    {
                        var FMSData = db.prc_ext_FMSRequirementData_Get(id).FirstOrDefault();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData.FMSRelatedReqInfoID = FMSData.FMSRelatedReqInfoID;
                        RelatedFMS.FMSData.FMSCountyCodeID = FMSData.FMSCountyCodeID;
                        RelatedFMS.FMSData.FMSCaseNumber = FMSData.FMSCaseNumber;
                        RelatedFMS.FMSManager = FMSData.FMSManager;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName", reqForEdit.RelatedFMSData.FMSData.FMSCountyCodeID);
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }
                    else
                    {
                        prc_FMS_Requirement_Data FMSData = new prc_FMS_Requirement_Data();
                        FMSDataModel RelatedFMS = new FMSDataModel();
                        RelatedFMS.FMSData = FMSData;
                        reqForEdit.RelatedFMSData = RelatedFMS;
                        var CountryList = db.prc_CountryCodes.ToList();
                        SelectList DDCountryCodeList = new SelectList(CountryList, "CountryCodeID", "CountryName");
                        reqForEdit.RelatedFMSData.DDCountyCodeList = DDCountryCodeList;
                    }

    Now the SelectList is beinbg generated and populated.

    For the second error:

    The stored Procedure being run to update the model, calls a second stored procedure, which is erroring out with an inability to convert varchar to int.

    This causes the view to be resubmitted with the model that was submitted to the edit.  However, now it is apparently missing the selectList property for StatusID.  Why this is the case I don't know yet.  I will need to research the code. 

    Update: 

    Turns out all of the DDL SelectList properties of the original model are null when the model is submitted to the Edit Post method.  While this would not be an issue when the Updates are successful, if there is an error and the view needs to be resubmitted, it is missing all of the SelectList properties needed to populate the DropDownListFor's

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 14, 2014 12:55 PM
  • User-1842880510 posted

    But here is an unexpected issue.  The model I am passing consist of a property for the main table which contains my data plus some SelectList properties and some Related sets of data.  However, the base table has some virtual properties representing tables with which it has a foreign key relationship to.  But these related table object virtual properties are not being passed to the Post method either.  This is surpising because as part of the original table's model I would have expected these properties to remain populated.  And In fact these values are filled in when the original table's object is created by calling db.tablename.Find(id);

    Fortunately I am only using values from these related tables of the Model in one place in the scripting, so I replaced that with a JQuery call to the selected value's text in that one place.  so I am no longer getting this error.

    I am still curious as to how one would go about repopulating the those virtual related table object properties.

    Wednesday, May 14, 2014 3:33 PM