TextboxFor传递文本,但RadioButtonFor不传递

本文关键字:RadioButtonFor 文本 TextboxFor | 更新日期: 2023-09-27 18:17:45

我试图传递一个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; }
}
<<p> 视图/strong>
@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"

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

TextboxFor传递文本,但RadioButtonFor不传递

你可以使用@Html。但是你应该确保项。CommentTypeDesc兼容Radio类型

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

希望能有所帮助。