0

I'm trying to pass a RadioButtonFor to the model.

Controller

[HttpPost]
    public ActionResult Contact(ApplicationCommentType model)
    {
        //send email here

        //reload form
        ApplicationCommentType appdata = new ApplicationCommentType();
        appdata.CommentTypeData = db.CommentTypes.ToList();
        return View(appdata);
    }  

ApplicationCommentType

public class ApplicationCommentType
{
    public IEnumerable<CommentType> CommentTypeData { get; set; }
    public String CommentTypeDataSelection { get; set; }
    public String Name { get; set; }
    public String Email { get; set; }
    public String Comment { get; set; }
}

CommentType

public partial class CommentType
{
    public int CommentTypeID { get; set; }
    public string CommentTypeDesc { get; set; }
}

View

@using(@Html.BeginForm("Contact", "Home", FormMethod.Post, new{ @class ="form-horizontal"})){
            <fieldset>
                <legend>Contact Us</legend>
                <div class="form-group">
                    @Html.LabelFor(x => x.Email, new {@class="col-lg-2 control-label"})
                    <div class="col-lg-10">
                        @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })                            
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.Name, new { @class = "col-lg-2 control-label" })
                    <div class="col-lg-10">
                        @Html.TextBoxFor(x => x.Name, new { @class = "form-control" }) 
                    </div>
                </div>
                <div class="form-group">
                    <label for="textArea" class="col-lg-2 control-label">Questions, Comments, or Concerns</label>
                    <div class="col-lg-10">
                        @Html.TextAreaFor(x => x.Comment, new { @class = "form-control" }) 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">Comment Type</label>
                    <div class="col-lg-10">               
                        @foreach (var item in Model.CommentTypeData)
                        {
                            <div class="radio">
                                <label>
                                    @Html.RadioButtonFor(x => x.CommentTypeData, item.CommentTypeDesc)
                                    @Html.LabelFor(m => m.CommentTypeData, item.CommentTypeDesc, item.CommentTypeID)                                        
                                </label>
                            </div>
                        }      
                        @Html.HiddenFor(x => x.CommentTypeDataSelection)         
                    </div>
                </div>
           </fieldset>
}

Now this kind of works, all the textbox items work. Placing a break point on the [HttpPost] return yields the following values.

    Comment: "awesome"
    CommentTypeData: Count = 0
    CommentTypeDataSelection: null
    Email: "example@example.com"
    Name: "John Smith"

Shouldn't CommentTypeData have a count? If I check the request the selected value is there.

Request.Params["CommentTypeData"]: "General Improvement Suggestion"

So why is the Model not updated? Is it a requirement to manually update the Model from the Request object?

atrueresistance
  • 1,358
  • 5
  • 26
  • 48
  • Your creating a radio button group which only only posts back a single value (the value of the selected radio button. It cannot be bound to a collection of complex objects. Not sure what your actually trying to do with this? Best guess is you want `@Html.RadioButtonFor(x => x.CommentTypeDataSelection , item.CommentTypeDesc)` –  Oct 13 '15 at 21:46
  • And `CommentTypeData` will always be an empty collection because you don't (and should not be) generating inputs in the view for each property of each `CommentType` in the collection –  Oct 13 '15 at 23:41

1 Answers1

0

You can use @Html.RadioButtonFor but you should make sure that item.CommentTypeDesc compatible with Radio type.

Refer to MVC4: Two radio buttons for a single boolean model property

Hope it helps.

Community
  • 1
  • 1
Lewis Hai
  • 1,114
  • 10
  • 22