locked
Need help setting up multi select option checkbox list for my ASP.NET Core Razor Website RRS feed

  • Question

  • User-1880744392 posted

    I have been trying several code samples from forums but they all seem to use a List of a class type that gets data saved in, while I am just trying to use a list of strings from data for the days of the week, so users can choose their preferred days of the week for personal training.

    Here are some of the few things I have tried:

    Model I have tried using 

            public List<string> DailyAvail { get; set; } = new List<string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    

    PageModel

            public List<string> DailyAvail { get; set; } = new List<string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    
            public async Task<IActionResult> OnPostAsync()
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }
                var emptySession= new TrainingRoutineVM();
                var entry = _context.Add(emptyPtSession);
                entry.CurrentValues.SetValues(TrainingRoutineVM);
                await _context.SaveChangesAsync();
    
                PopulatePtDropDownList(_context, emptyPtSession.PersonalTrainingID);
    
                return RedirectToPage("./Index");
            }

    View

                    <label class="form-check-label">
                        <ul>
                        @for (var i = 0; i < this.Model.DailyAvail.Count(); i++)
                        {
                            <li>
                                <input type="checkbox" asp-for="@Model.TrainingRoutineVM.PersonalTraining.Product.IsChecked" />
                                <label asp-for="@Model.TrainingRoutineVM.PersonalTraining.Product.IsChecked">@Model.DailyAvail[i].</label>
                                <input type="hidden" asp-for="@Model.TrainingRoutineVM.TrainingRoutineID" />
                                <input type="hidden" asp-for="@Model.DailyAvail[i]" />
                                @Html.ValidationMessageFor(x => x.DailyAvail)
                            </li>
                        }
                        </ul>
                    </label>

    which gave me an entity relationship error when I try to add migration because List<string> is not compatible with EF.

    Then I tried Using this in the page model and also view Model only but i think then it will not save the results onto the database, if the properties do not exist in both the model and the view model, so I was thinking of trying to add this in the model with a string property with the same name as the List item in view model

    public string DailyAvail { get; set; }

    But I am not sure how to dynamically convert into type string when adding to the database, I think when seeding the database I did something like:

    DailyAvail =  string.Join(",", DailyAvail);

    but yeah when adding the list items in the database I am not sure how to do that.

    So yeah how would I implement an option to multi select days of the week, so user can select a couple of options, which is supposed to be saved to the user's order when they checkout.

    Tuesday, June 2, 2020 12:41 AM

All replies

  • User2078676645 posted

    Hi,

    I don’t know whether you save these data in multiple columns of a table or other forms. In this way, you can add multiple choices to the Razor page.

    @page
    @model IndexModel
    @{
        SelectListItem item;
        List<SelectListItem> list = new List<SelectListItem>();
        for (int i = 0; i < this.Model.DailyAvail.Count(); i++)
        {
            item = new SelectListItem();
            item.Text = this.Model.DailyAvail[i];
            item.Value = this.Model.DailyAvail[i];
            list.Add(item);
        }
    }
    <form asp-page-handler="getlistdaily" method="post">
        @Html.ListBox("dailylist", list)
        <input type="submit" value="sub" />
    </form>

    Then, it will send the request to the method of getlistdaily in pageModel.

    public void OnPostGetlistdaily(List<string> dailylist)
            {
                foreach(var t in dailylist)
                {
                    Console.WriteLine(t);
                }
                //save your data with the type of string concatenation or array
            }

    Regards,

    Evern

    Tuesday, June 2, 2020 2:46 AM