0

我正在尝试将 RadioButtonFor 传递给模型。

控制器

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

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

应用评论类型

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; }
}

评论类型

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: "example@example.com"
    Name: "John Smith"

CommentTypeData 不应该有计数吗?如果我检查请求,则选择的值在那里。

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

那么为什么模型没有更新呢?是否需要从请求对象手动更新模型?

4

1 回答 1

0

您可以使用 @Html.RadioButtonFor 但您应该确保item.CommentTypeDesc与 Radio 类型兼容。

请参阅MVC4:单个布尔模型属性的两个单选按钮

希望能帮助到你。

于 2015-10-14T04:48:14.997 回答