Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>