Home How to bind MVC checkboxes to model
Reply: 0

How to bind MVC checkboxes to model

user8348
1#
user8348 Published in September 21, 2018, 8:35 am

This question already has an answer here:

  • Pass List of Checkboxes into View and Pull out IEnumerable [duplicate] 2 answers
  • Updating a list of objects in mvc4 1 answer

I have a view which outputs a list of questions. The user will select questions they want included in a report and then submit the form.

My view looks like:

@using (Html.BeginForm("Step6", "Home", FormMethod.Post))
{
  @Html.HiddenFor(m => m.EventID)
  @Html.HiddenFor(m => m.CompanyID)
  foreach (var item in Model.Questions)
  {
      <div class="checkbox">
      <input type="checkbox" id="@item.QuestionID" name="QuestionIds" value="@item.Title" />
      <label for="@item.QuestionID">@item.Title</label>
      </div>
  }
  <p><input type="submit" class="btn btn-primary" value="Submit" /></p>
}

Generated HTML looks like:

<div class="checkbox">
    <input type="checkbox" id="12" name="QuestionIds" value="Would you like an account manager to contact you?" />
    <label for="12">Would you like an account manager to contact you?</label>
</div>
<div class="checkbox">
    <input type="checkbox" id="13" name="QuestionIds" value="Comments - please be as detailed as possible." />
    <label for="13">Comments - please be as detailed as possible.</label>
</div>

Question collection:

public class Question
{
    public Guid QuestionID { get; set; }
    public string Title { get; set; }
}

Controller action

    [HttpPost]
    public ActionResult Step6(Models.EventCompanyQuestionnaireQuestions model)
    {
        return View();
    }

The model:

public class EventCompanyQuestionnaireQuestions
{
    public int EventID { get; set; }
    public int CompanyID { get; set; }
    public List<Guid> QuestionIds { get; set; }
}

When the form is submitted the List is an empty, initialized list. The rendered form element is named QuestionIds which matches the model. I need QuestionIds to have the checked checkboxes.

share|improve this question

marked as duplicate by Fran, Sam Axe c# StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover = $(this).addClass('hover-bound'), $msg = $hover.siblings('.dupe-hammer-message'); $hover.hover( function() { $hover.showInfoMessage('', { messageElement: $msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Feb 13 at 20:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • There a numerous answers on SO for this – Fran Feb 13 at 19:40

2 Answers 2

active oldest votes
up vote -1 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326283 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO