Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:27:30+00:00 2026-05-26T13:27:30+00:00

MVC 3 VB.NET razor. I have a view that has 4 dropdown boxes in

  • 0

MVC 3 VB.NET razor. I have a view that has 4 dropdown boxes in it.. This is for setting up a staff member.. If that staff member is to work certain classes then that class will be set for each day. If he is not then the value needs to stay null. This is a edit view so it may have to be accessed multiple times and still keep the original selectlist values if none changed. The below is what I have right now that is working only on its face. The Old selected value is shown first. However this isnt being returned on the save… The only way that it will save correctly is if I select the value that was set in each box then click save. The next problem is that not all staff members will have classes to work on every one of the 4 days. So how do I set a value to null and keep it that way unless a class Is actually selected..

    Dim _staff As confstaff = db.confstaffs.Single(Function(a) a.id = id)
        ViewBag.role = _staff.Conf_Role.ToString
        ViewBag.confRole = db.conf_roles.ToList
        ViewData("tue_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Tuesday").Select(Function(r) r.course_ref), New With {.value = _staff.tue_class})
        ViewData("wed_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Wednesday").Select(Function(r) r.course_ref), New With {.value = _staff.wed_class})
        ViewData("thur_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Thursday").Select(Function(r) r.course_ref), New With {.value = _staff.thur_class})
        ViewData("fri_Class") = New SelectList(db.courses.ToList.Where(Function(r) r.course_day = "Friday").Select(Function(r) r.course_ref), New With {.value = _staff.fri_class})
        Return View(_staff)

And the view is:

     <label>Tuesday Class</label>
       @Html.DropDownList("tue_class", "Select One")
     <label class="small_spacing">Wednesday Class</label>
        @Html.DropDownList("wed_class", "Select One")
     <label class="small_spacing">Thursday Class</label>
        @Html.DropDownList("thur_class", "Select One")
     <label class="small_spacing">Friday Class</label>
        @Html.DropDownList("fri_class", "Select One")

I already expect someone to point out that I should use a view model instead of viewbag but I dont see how a view model would be practical with there being over 100 different courses but I am open for ideas…

Any Ideas???????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:27:31+00:00Added an answer on May 26, 2026 at 1:27 pm

    if you look at your page source after the page is loaded you will see your top item set from your viewbag value has no value only text so when it is submitted it thinks you are submitting blank. I ran into this before.

    What you need to do is to manually create each of your dropdown lists by iterating through the collection and setting the one that matches your viewbag item to selected, that way you can be sure a selected item has a selected value. I have some Razr code around here somewhere. Will update when I find it.

    EDIT

        <select name="Type1" id="Type1">
    
        <option value=""></option>
         @foreach (var name in ViewBag.BackOfficeTypes)
         {
             if (name == ViewBag.SelectedType1Value)
             {
                 <option value="@name" selected="selected">@name</option>
             }
             else
             {
                     <option value="@name">@name</option> 
             }
         }
         </select> 
    

    I have several of these on the same page building from different sets of items. Hope this helps.

    EDIT 2

    If you want to do it all from codebehind I am not a VB guy but here is a way to do it but you need to change your linq statement to manually create the list items instead of dumping from the toList Method.

    var courses = db.getCourses();
       IEnumerable<SelectListItem> selectList = 
        from c in courses
        where c.course_day = "Tuesday"
        select new SelectListItem
        {
            Selected = (c.CourseID == selectedCourseID),
            Text = c.Name,
            Value = c.CourseID.ToString()
        };
    

    If you can translate this into the VB equivalent it might solve your issue instead of building them in the Razor end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 3 view using Razor and Knockout.js that is not
I have an ASP.NET MVC website which has a particular javascript file that needs
I am using ASP.NET MVC 3 with the Razor view engine . I have
I have converted my classic asp.net mvc views to razor. In a view there
I've got a large site that runs in ASP.NET MVC using the Razor view
MVC 3 VB.NET applicaiton... Using express checkout with html razor view. I have tried
I am using ASP.Net MVC 3 Razor view engine. I have a requirement to
ASP.NET MVC has been discussed on this forum a few times. I'm about to
Do asp.net aspx views have tags that that work similar to the Ruby erb
I have a ASP.Net MVC app with Razor. I try to access a collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.