我说我渲染一个复选框:
@Html.CheckBoxFor(x => x.Checked) // Checked is true by default
ASP会把它改为:
<input checked="checked" data-val="true" data-val-required="The field is required." id="Checked" name="Checked" type="checkBox" value="true" /> <input name="Checked" type="hidden" value="false" />
由于ASP为复选框输出两个具有相同名称的输入,所以在提交表单时,还会在URL中获取两个GET参数:
http://...?Checked=true&Checked=false
让我们说我也使用MvcContrib来显示一个排序表.
当我对列进行排序时,MvcContrib无法理解重复的GET参数,而不是写入?Checked = true& Checked = false,它写入?Checked = true,false,不能被MVC3解析为bool.排序后的错误信息为:
String was not recognized as a valid Boolean.
有没有人遇到这个问题与MvcContrib网格?
解决方法
好的,想想我已经想出了解决方案:
创建自己的HtmlTableGridRenderer:
public class CustomTableGridRenderer<Tviewmodel> : HtmlTableGridRenderer<Tviewmodel> where Tviewmodel : class { protected override void RenderHeaderText(GridColumn<Tviewmodel> column) { if (IsSortingEnabled && column.Sortable) { string sortColumnName = GenerateSortColumnName(column); bool isSortedByThisColumn = GridModel.SortOptions.Column == sortColumnName; var sortOptions = new GridSortOptions { Column = sortColumnName }; if (isSortedByThisColumn) { sortOptions.Direction = (GridModel.SortOptions.Direction == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending; } else //default sort order { sortOptions.Direction = column.InitialDirection ?? GridModel.SortOptions.Direction; } var routeValues = CreateRouteValuesForSortOptions(sortOptions,GridModel.SortPrefix); //Re-add existing querystring foreach (var key in Context.RequestContext.HttpContext.Request.QueryString.AllKeys.Where(key => key != null)) { if (!routeValues.ContainsKey(key)) { routeValues[key] = Context.RequestContext.HttpContext.Request.QueryString[key]; } } var link = HtmlHelper.GenerateLink(Context.RequestContext,RouteTable.Routes,column.DisplayName,null,routeValues,null); RenderText(link); } else { base.RenderHeaderText(column); } } }
…只是替换
if(! routeValues.ContainsKey(key)) { routeValues[key] = Context.RequestContext.HttpContext.Request.QueryString[key]; }
… with routeValues [key] = Context.RequestContext.HttpContext.Request.QueryString [key];
并使用您的新渲染:
@ Html.Grid()… RenderUsing(new CustomTableGridRenderer())