How do I render a group of checkboxes using MVC 4 and View Models (strongly typed)

后端 未结 3 1106
谎友^
谎友^ 2021-02-01 06:35

I\'m rather new to the ASP.net MVC world and I\'m trying to figure out how to render a group of checkboxes that are strongly typed to a view model. In webforms I would just use

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 07:31

    You could enrich your view model:

    public class VendorAssistanceViewModel
    {
        public string Name { get; set; }
        public bool Checked { get; set; }
    }
    
    public class ContactViewModel
    {
        public ContactViewModel()
        {
            VendorAssistances = new[]
            {
                new VendorAssistanceViewModel { Name = "DJ/BAND" },
                new VendorAssistanceViewModel { Name = "Officiant" },
                new VendorAssistanceViewModel { Name = "Florist" },
                new VendorAssistanceViewModel { Name = "Photographer" },
                new VendorAssistanceViewModel { Name = "Videographer" },
                new VendorAssistanceViewModel { Name = "Transportation" },
            }.ToList();
        }
    
        [Required]
        public string Name { get; set; }
    
        [Required]
        public string Phone { get; set; }
    
        [Required]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }
    
        [Required]
        public string Subject { get; set; }
        public IEnumerable SubjectValues
        {
            get
            {
                return new[]
                {
                    new SelectListItem { Value = "General Inquiry", Text = "General Inquiry" },
                    new SelectListItem { Value = "Full Wedding Package", Text = "Full Wedding Package" },
                    new SelectListItem { Value = "Day of Wedding", Text = "Day of Wedding" },
                    new SelectListItem { Value = "Hourly Consultation", Text = "Hourly Consultation" }  
                };
            }
        }
    
        public IList VendorAssistances { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new ContactViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(ContactViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
    
            //Send email logic
            return RedirectToAction("ContactConfirm");
        }
    }
    

    View:

    @using (Html.BeginForm())
    {
        
    Your Name:
    @Html.TextBoxFor(model => model.Name)
    Your Phone:
    @Html.TextBoxFor(model => model.Phone)
    Your Email:
    @Html.TextBoxFor(model => model.Email)
    Subject:
    @Html.DropDownListFor(model => model.Subject, Model.SubjectValues)
    Vendor Assistance:
    @for (int i = 0; i < Model.VendorAssistances.Count; i++) {
    @Html.HiddenFor(x => x.VendorAssistances[i].Name) @Html.CheckBoxFor(x => x.VendorAssistances[i].Checked) @Html.LabelFor(x => x.VendorAssistances[i].Checked, Model.VendorAssistances[i].Name)
    }
    }

提交回复
热议问题