locked
Converting mvc razor C-Sharp to VB.NET razor RRS feed

  • Question

  • User2130491911 posted

    So for about 3 weeks not I have been having problems making a drop down list in the mvc.

    what is beyond frustrating is that I have this working in one of the c# applications that I created.

    Now I need to use that data structure in a VB.Net application but sadly all usable examples are in the c-shape flavor.

    I made a post here a few days ago that no one could give me clear guidance on so now looking at some of my other application built in c-sharp I notice that I have done the thing there. 

    For some reason the c-shape converts can't do the razor conversion so maybe some guru here can help

    I will post what I have working in the c-sharp followed by what I need to work in the Vb.nets

    The Razor in the C-sharp

                @Html.DropDownListFor(model => model.Company_ID, (SelectList)ViewBag.CompanyList, new { @class = "form-control", @id = "COMPANY_DLL" })



                        //Populate Drop Downs List
                        //ViewBag.CompanyList = new SelectList(CUPS_DDL.GetCAMSCompanys(), "Value", "Text", model.Company_ID);
                        ViewBag.CompanyList = new SelectList(Enumerable.Empty<SelectListItem>());



    The Razor stuff
    @Html.DropDownListFor(model => model.JWS_CONTEST_ID, new SelectList(ViewBag.ContestList, "CT_CONTEST_ID", "CT_CONTEST_DESCRIPTION"))
    
    
    
    
    The C-sharp stuff
                var Contest_List = db.CT_CONTEST_MASTER_TBL.ToList();
    
                if (Contest_List != null)
                {
                    ViewBag.ContestList = Contest_List;
                }

    So the above works and in this case the razor stuff allows me to tell the computer to use CT_CONTEST_ID", "CT_CONTEST_DESCRIPTION as the Value and text I guess

    The other C-sharp razor code does not have the Value and Text Listed out , THis one

     @Html.DropDownListFor(model => model.Company_ID, (SelectList)ViewBag.CompanyList, new { @class = "form-control", @id = "COMPANY_DLL" })

    If I am honest I am not 100% sure how these work as I have yet to find any clear guidance on this. I just use my working examples, But now I need to convert this functionality to VB.Nets and there is no clear guidance. I was hoping someone could help me translate this from C-sharp to VB.net

    My VB.Net Examples:

     The VB.NEts Razor stuff (works But only shows the table names vs Value and Text Info
    
                   @Html.DropDownListFor(Function(model) model.Code_Reference, CType(ViewBag.RefCodesList, SelectList))
    
    The VB.net COde
    
    Make the List 
            'GET CLE-LIST
            Function GET_CODE_REF_List() As List(Of CODE_REF_LOV_TBL)
    
    
                'Fetch Records From Table using EF
                Dim REF_CODES_LIST As List(Of CODE_REF_LOV_TBL) = (From CODE In db.CODE_REF_LOV_TBL Select CODE).ToList()
    
    
                'Insert The Defau't Item To the LOV
                REF_CODES_LIST.Insert(0, New CODE_REF_LOV_TBL With {.CODE_REF_REC_ID = "0", .CODE_DESCRIPTION = "PLEASE SELECT"})
    
    
                Return (REF_CODES_LIST)
    
            End Function
    
    
    
    
    
    Connect the list to viewbag
            ' GET: CO/Create
            Function Create() As ActionResult
    
                'Get LOV
                'Dim customerList As List(Of SelectListItem) = GET_CODE_REF_List()
                ViewBag.RefCodesList = New SelectList(GET_CODE_REF_List())
    
    
                Return View()
            End Function

    This may make it clearer 

    So this may be a better question to ask show that I have this stuff working in a different language. But now I am trying to get it work in another that I have been a way from for a while (Never did the mvc in VB.net) So I am having trouble getting the concepts to work since for some reason the example in VB.Net are even worst then the C-sharp examples.

    Tuesday, May 18, 2021 4:07 PM

Answers

  • User2130491911 posted

    mgebhard

    Your Html.DropDownListFor overload is incorrect.  It's just...

    @Html.DropDownListFor(Function(model) model.CourseID, CType(ViewBag.RefCodesList, List(Of SelectListItem)))

    This assumes you populated ViewBag.RefCodesList correctly which I cannot verify.

    Function Index() As ActionResult
    
        ViewBag.RefCodesList = GET_CODE_REF_List()
        Return View()
    
    End Function

    THis is my error sadly

    System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[BFR_LOCAL_DB.CODE_REF_LOV_TBL]' to type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]'.'

    It just may be time for me to give up on this. Clearly something is wrong with the mvc and it can no long support dropdownlist for vb.net

    Yeah as I stated on the other post. I will just put the values in a onscreen table and the user will have to enter them in by typing them

    This is way too much of a headache lol Dropdownlist in VB.net is way too hard of a concept fro me on this day. Maybe if I have to revisit this in the future I will try it again Thanks All for trying.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2021 9:06 PM

All replies

  • User2130491911 posted

    Ok after making some changes based of of this very unhelpful write up

    https://visualstudiomagazine.com/articles/2011/09/01/pcovb_razor-vb/listing4.aspx

    I did the following changes 

    I changed this

                    @Html.DropDownListFor(Function(model) model.Code_Reference, CType(ViewBag.RefCodesList, List(Of SelectListItem)))

    To This but now I get just the word Selectlistitem repeated lol (So frustrating this thing lol

                    @Html.DropDownListFor(Function(model) model.Code_Reference, ViewBag.RefCodesList)

    But I also changed my function that makes my list and now  it causes this to error our

                    @Html.DropDownListFor(Function(model) model.Code_Reference, CType(ViewBag.RefCodesList, List(Of SelectListItem)))

    This has a error of when Running:

    System.InvalidCastException: 'Unable to cast object of type 'System.Web.Mvc.SelectList' to type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]'.'

    But I am guessing because I am changing the the way the list is created??

    I was creating this way based on the write up

            'GET CLE-LIST
            Function GET_CODE_REF_List() As List(Of SelectListItem)
    
    
                'Fetch Records From Table using EF
    
                Dim REF_CODES_LIST = (From c In db.CODE_REF_LOV_TBL Select New SelectListItem With {.Text = c.CODE_DESCRIPTION, .Value = c.CODE_REF_REC_ID}).ToList()
    
                'Dim REF_CODES_LIST As List(Of CODE_REF_LOV_TBL) = (From CODE In db.CODE_REF_LOV_TBL Select CODE).ToList()
    
    
                'Insert The Defau't Item To the LOV
                'REF_CODES_LIST.Insert(0, New CODE_REF_LOV_TBL With {.CODE_REF_REC_ID = "0", .CODE_DESCRIPTION = "PLEASE SELECT"})
    
    
                Return REF_CODES_LIST
    
            End Function

    At this point I am trying anything I can find and I just keep getting errors 

    This Yields 

        @Html.DropDownListFor(Function(model) model.Code_Reference, CType(ViewBag.RefCodesList, List(Of SelectListItem)), "CODE_REF_REC_ID", "CODE_DES")

    THis error 

    System.InvalidCastException: 'Unable to cast object of type 'System.Web.Mvc.SelectList' to type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]'.'

    This is promising :

                    @Html.DropDownListFor(Function(model) model.Code_Reference, ViewBag.RefCodesList)

    But all it list is this for every entry in the list:

    system.web.mvc.selectlistitem

    It this last change tells me I am close but computer is not telling me what to do. All the examples claim this should work but it does not. so I am guessing I have the VB converted but I am back a square one lol

    Really not sure what it happening.  Any insight would be great.

    Tuesday, May 18, 2021 4:59 PM
  • User475983607 posted

    Your Html.DropDownListFor overload is incorrect.  It's just...

    @Html.DropDownListFor(Function(model) model.CourseID, CType(ViewBag.RefCodesList, List(Of SelectListItem)))

    This assumes you populated ViewBag.RefCodesList correctly which I cannot verify.

    Function Index() As ActionResult
    
        ViewBag.RefCodesList = GET_CODE_REF_List()
        Return View()
    
    End Function

    Tuesday, May 18, 2021 7:05 PM
  • User2130491911 posted

    mgebhard

    Your Html.DropDownListFor overload is incorrect.  It's just...

    @Html.DropDownListFor(Function(model) model.CourseID, CType(ViewBag.RefCodesList, List(Of SelectListItem)))

    This assumes you populated ViewBag.RefCodesList correctly which I cannot verify.

    Function Index() As ActionResult
    
        ViewBag.RefCodesList = GET_CODE_REF_List()
        Return View()
    
    End Function

    THis is my error sadly

    System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[BFR_LOCAL_DB.CODE_REF_LOV_TBL]' to type 'System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]'.'

    It just may be time for me to give up on this. Clearly something is wrong with the mvc and it can no long support dropdownlist for vb.net

    Yeah as I stated on the other post. I will just put the values in a onscreen table and the user will have to enter them in by typing them

    This is way too much of a headache lol Dropdownlist in VB.net is way too hard of a concept fro me on this day. Maybe if I have to revisit this in the future I will try it again Thanks All for trying.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2021 9:06 PM