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 7568547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:57:13+00:00 2026-05-30T14:57:13+00:00

I have this problem where i want to make 7 dropdowns for each day

  • 0

I have this problem where i want to make 7 dropdowns for each day of the week.
In each one of those dropdowns i wish to add the same data.

My ViewModel:

public class WeekDienstCreateViewModel
{
    public WeekDienst weekDienst {get; set;}
    public List<DienstPerWeekDienst> diensten { get; set; }

    public WeekDienstCreateViewModel() { }
}

My Create Method in Controller:
As u can see I add everything allready except DienstId which is want to add with my dropdowns.

 public ActionResult Create(int id)
        {
            WeekDienst wd = _service.FindWeekDienst(id);
            WeekDienstCreateViewModel vm = new WeekDienstCreateViewModel();

            vm.diensten = new List<DienstPerWeekDienst>();
            vm.weekDienst = wd;

            for (int i = 1; i <= 7; i++)
            {
                DienstPerWeekDienst dpwd = new DienstPerWeekDienst();
                dpwd.volgnummer = i;
                dpwd.WeekDienstId = wd.Id;

                vm.diensten.Add(dpwd);
            }
            ViewBag.Diensten = _service.DienstenList(wd.AfdelingId);

            return View(vm);
        } 

Classes:

 public class DienstPerWeekDienst
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public int WeekDienstId { get; set; }

        [Required]
        public int DienstId { get; set; }

        [Required]
        [Range(1, 7)]
        public int volgnummer { get; set; }

        [ForeignKey("WeekDienstId")]
        public virtual WeekDienst WeekDienst { get; set; }

        [ForeignKey("DienstId")]
        public virtual Dienst Dienst { get; set; }

        public virtual ICollection<WeekDienst> WeekDiensten { get; set; }
    }

 public class WeekDienst
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public int AfdelingId { get; set; }

        [Required]
        [StringLength(5, ErrorMessage = "Value for {0} cannot exceed {1} characters.")]
        [RegularExpression(@"^[a-zA-Z0-9]{5}$", ErrorMessage = "Verplicht 5 cijfers lang.")]
        public string code { get; set; }

        [DisplayName("Template")]
        public bool template { get; set; }

        [ForeignKey("AfdelingId")]
        public virtual Afdeling Afdeling { get; set; }
    }

And in my view i wish to create 7 dropdowns where i put in all my “Diensten” (class Dienst, fk in DienstPerWeekDienst). When I choose 1 i wish to add the “DienstId” into the “DienstPerWeekDienst” class.

So in my View i got this:

            @foreach (var day in Model.diensten)                 
             {   
                  var currentDay=day;
                  @Html.DropDownListFor(currentDropDown=>currentDay, new SelectList(ViewBag.Diensten, "Value", "Text"))                 
             }

I Wish to postback the chosen “Diensten” and create the “WeekDienst” but now i am just posting a null “DienstPerDienstWeekCreateViewModel”. How am I able to fix this?

Thanks in Advance

FIX (Thanks to Siva Gopal)

I fixed this by doing:

@for (int i = 0; i < @Model.diensten.Count; i++)
                {
                    @Html.HiddenFor(m => (m.diensten[i].volgnummer))
                    @Html.HiddenFor(m => (m.diensten[i].WeekDienstId))
                    @Html.DropDownListFor(m=> (m.diensten[i].DienstId), new SelectList(ViewBag.Diensten, "Value", "Text"))
                }
  • 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-30T14:57:14+00:00Added an answer on May 30, 2026 at 2:57 pm

    You may try using

    @foreach (var day in Model.diensten)                 
     {   
          var currentDay=day;              
          @Html.DropDownListFor(currentDropDown=>currentDay, new SelectList(ViewBag.Diensten, "PropertyName_Holding_Value", "PropertyName_Holding_DisplayText"), new { })                 
     }  //This uses the Lambda Expression. Your dropdown Name/Id would be 1,2,3 etc. based on currentDay value.
    

    OR

    @foreach (var day in Model.diensten)                 
     {   
          var currentDay=day;
          var dropdownName=string.Format("diensten[{0}]",day-1); //If you want to model bind the selected dropdown value to input entity in POST request. The final dropdownName format should match the hierarchy of the property inside input entity/object. Even without this name formation, you can still POST the selected value back using Jquery/Javascript.
          @Html.DropDownList(dropdownName, new SelectList(ViewBag.Diensten, "PropertyName_Holding_Value", "PropertyName_Holding_DisplayText"), new {})                 
     }  //
    

    Note for Value Post back/model bind on full Page submit:
    To be able to model bind/POST back values to the server, the html element names corresponding to the properties should be rendered as follows: Suppose if you display Employee.Department.Name, then name of textbox, displaying the Department Name in View should match Department_ReferenceName_Inside_Employee.Name for model binding.

    Model:
    public class Employee
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public Department EmpDepartment { get; set; }
    public List SubOrdinates { get; set; }
    }
    public class Department
    {
    public string Name { get; set; }
    }

    Controller:

    public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Welcome to ASP.NET MVC!";
    
                //Prepare the model and send it to the view
                Employee emp = new Employee { EmpDepartment = new Department { Name = "IT" } };
                emp.SubOrdinates = new List<Employee> { new Employee { Name = "Emp1" }, new Employee { Name = "Emp2" } };
                return View(emp);
            }
    
            [HttpPost]
            public ActionResult Index(Employee emp)
            { //Put a break-point here and see how the modified values in view are flowing into emp..
                return View(emp); 
            }
            public ActionResult About()
            {
                return View();
            }
        }
    

    View:

    @model MvcApplication.Models.Employee
    @using (Html.BeginForm())
    {
    
        @Html.TextBoxFor(m => m.EmpDepartment.Name)
        @Html.LabelForModel("SubOrdinates :")
        for (int i = 0; i < @Model.SubOrdinates.Count; i++)
        {
              @Html.TextBoxFor(m => (m.SubOrdinates[i].Name))
        }
    <input type="submit" name="name" value="Submit" />    }
    

    ViewSource/PageSource:
    The above text box syntax will be rendered as :

    <input id="EmpDepartment_Name" name="EmpDepartment.Name" type="text" value="IT" />    <!--See above html : name=EmpDepartment.Name --> 
    <label for="">SubOrdinates :</label>  
    <input id="SubOrdinates_0__Name" name="SubOrdinates[0].Name" type="text" value="Emp1" />  
    <input id="SubOrdinates_1__Name" name="SubOrdinates[1].Name" type="text" value="Emp2" />  <!--See above html for how collection item Name(s) are being renderd by view engine-->     
    <input type="submit" name="name" value="Submit" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to .net and have this problem: I want to make installer
I have this problem for my social network website. I want to make an
I have this problem. I want to add image to listView. Exactly I want
I have this mysql tables I want to display with jqgrid. The problem appears
I came across this problem in javabat( http://www.javabat.com/prob/p183562 ): We want to make a
I have this problem that i want to transfer specific ( yet not that
I have a problem in my jquery code. I want to make a cascading
I have this problem understanding how to save data in order to have an
I have this problem I'm hoping someone knows the answer to. I have an
I have this problem where I open Visual Studio and the internal windows are

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.