您是使用MVC的ValidationMessage助手验证客户端还是在控制器中验证客户端?如果您发布页面并使用模型状态.IsValid在控制器上,确保返回具有相同实体的视图。
是否将默认单选按钮设置为on文件准备就绪(…)或使用JQuery/Javascript在客户端加载(…)?否则,我已经测试了以下内容,它工作正常,单选按钮状态保持不变:
public class MyModel
{
[Required(ErrorMessage="Name is required")]
public string Name { get; set; }
public bool IsSelected { get; set; }
}
查看:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestSample.Models.MyModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%Html.BeginForm(); %>
<%Html.EnableClientValidation(); %>
<p>
<%=Html.RadioButtonFor(m=>m.IsSelected,"true") %>
<%=Html.RadioButtonFor(m=>m.IsSelected,"false") %>
<%=Html.TextBoxFor(m => m.Name) %>
<%=Html.ValidationMessageFor(m => m.Name, "Required") %>
<input type="submit" id="btnSubmit" name="name" value="Submit" />
</p>
<%Html.EndForm(); %>
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</asp:Content>
[HttpPost]
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{//Your code goes here }
return View(model);
}