0
我正在尝试将RadioButtonFor传递给模型。TextboxFor传递文本,但RadioButtonFor不
控制器
[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; }
}
查看
@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>
}
现在这种作品,所有的文本框的项目工作。在[HttpPost]
返回上放置一个断点将产生以下值。
Comment: "awesome"
CommentTypeData: Count = 0
CommentTypeDataSelection: null
Email: "[email protected]"
Name: "John Smith"
不应该CommentTypeData有一个计数?如果我检查请求,那么所选值就在那里。
Request.Params["CommentTypeData"]: "General Improvement Suggestion"
那么为什么模型没有更新?是否需要从Request对象手动更新模型?
您创建的单选按钮组只能回传单个值(所选单选按钮的值,不能绑定到一组复杂对象,不确定您实际尝试使用的是什么?你需要'@ Html.RadioButtonFor(x => x.CommentTypeDataSelection,item.CommentTypeDesc)' –
而'CommentTypeData'将永远是一个空集合,因为你没有(也不应该)在视图中为每个集合中每个'CommentType'属性 –