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?