- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很抱歉这里有很多代码......我尽量让它尽可能短:
我写了(主要是从这里偷来的)一个 HtmlHelper 扩展来写出一个基于枚举的 RadioButton(分组)。我还设置了我的路线图,因此它使用枚举而不是“id”等。
我有两个枚举(对于这个例子)CurrencyType 和 StatusType
当我调用 URL/GBP/Open 时,我的 HtmlHelper 正常工作并设置单选按钮的值并选中 GBP 和 Open。
当我调用 URL/gbp/open 时 - Helper 仍然会检查值并且似乎可以正常工作...但是当调用 RadioButton().ToHtmlString() 时“已检查”不存在????使用默认路由,单选按钮也仍然设置正确???
如果您对返回的 SelectList 断点,那么您可以看到所选的选项设置正确,所以我有点困惑问题出在哪里?
Empty MVC3 website called Area36...Area 51 was taken ;)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Text;
namespace Area36.Models
{
public static class Extensions
{
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { ID = e, Name = e };
return new SelectList(values, "Id", "Name", enumObj);
}
public static void AppendFormatLine(this StringBuilder sb, string format, params object[] args)
{
sb.AppendFormat(format, args);
sb.AppendLine();
}
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string fieldSet)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var e = (TProperty)Enum.Parse(typeof(TProperty), metaData.Model.ToString());
var selectList = e.ToSelectList();
var sb = new StringBuilder();
if (selectList != null)
{
sb.AppendFormatLine("<fieldset><legend>{0}</legend>", fieldSet);
foreach (SelectListItem item in selectList)
{
var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
if (htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix != string.Empty)
id.Insert(0, string.Format("{0}_", htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix));
var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
var radio = htmlHelper.RadioButton(name: metaData.PropertyName, value: item.Value, isChecked: item.Selected, htmlAttributes: new { id = id }).ToHtmlString();
sb.AppendFormatLine("<div class=\"radio_{0}\">{1}{2}</div>", metaData.PropertyName, radio, label);
}
sb.AppendLine("</fieldset>");
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Area36.Models;
namespace Area36.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index(CurrencyType currency, StatusType status)
{
return View(new ViewModel { Currency = currency, Status = status });
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Area36.Models
{
public class ViewModel
{
public CurrencyType Currency { get; set; }
public StatusType Status { get; set; }
}
}
@using Area36.Models
@model Area36.Models.ViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.RadioButtonForEnum(m => m.Currency, "Currency")
@Html.RadioButtonForEnum(m => m.Status, "Status")
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{currency}/{status}", // URL with parameters
new { controller = "Home", action = "Index", currency = CurrencyType.EUR, status = StatusType.Closed} // Parameter defaults
);
}
最佳答案
我尝试了多种方法来解决这个问题。强制传递 UpperCase 值。最令人惊讶的是,当我如下所示为所有单选按钮传递“true”时,即使输出相同,按钮也只检查大写 url。
var radio = htmlHelper.RadioButton(metaData.PropertyName, item.Value, true, new { id = id }).ToHtmlString();
通过使用此代码和一些谷歌搜索,我找到了 this .如果你检查以下是来自 MVC3-rtm 的代码。在这里你可以看到 isChecked 的值根据 ModelState 进行了修改,如果你在 Debug模式下检查 modelState.Values[index].AttemptedValue 的值,它显然是小写的(对于“gbp/open”)。这就是小写 url 不呈现选中属性的原因。
private IHtmlString BuildRadioButton(string name, object value, bool? isChecked, IDictionary<string, object> attributes) {
string valueString = ConvertTo(value, typeof(string)) as string;
TagBuilder builder = new TagBuilder("input");
builder.MergeAttribute("type", "radio", true);
builder.GenerateId(name);
builder.MergeAttributes(attributes, replaceExisting: true);
builder.MergeAttribute("value", valueString, replaceExisting: true);
builder.MergeAttribute("name", name, replaceExisting: true);
var modelState = ModelState[name];
string modelValue = null;
if (modelState != null) {
modelValue = ConvertTo(modelState.Value, typeof(string)) as string;
isChecked = isChecked ?? String.Equals(modelValue, valueString, StringComparison.OrdinalIgnoreCase);
}
if (isChecked.HasValue) {
// Overrides attribute values
if (isChecked.Value) {
builder.MergeAttribute("checked", "checked", true);
}
else {
builder.Attributes.Remove("checked");
}
}
AddErrorClass(builder, name);
return builder.ToHtmlString(TagRenderMode.SelfClosing);
}
关于c# - MVC3 LowerCase 路由值与 UpperCase 的行为不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7399357/
我的问题:我必须使用 ngSanitize 模块,但是当我运行该项目时,出现以下错误: Failed to instantiate module ngSanitize due to:lowercase
我在使用Electron的React w/react-photonkit绊了一下,这增加了一些不利因素。我正在尝试基于主侧菜单在光子header中创建动态水平菜单,以便在主要更改时触发水平选项进行更新
我有一个任务,描述如下: - Reads printable characters (20h-7Fh) from the keyboard without echoing - Uppercase le
在 Rails 3.2 应用程序中,我有一个 Tag模型,并希望找到 name:string 中值所在的所有记录字段全部小写。 因此 activerecord 查询(在 Postgres 上)将返回
我想知道如何将特定索引打印为大写和小写? 我不知道特定索引会发生变化吗? 对于单个索引,这是代码... public class UpperAndLowerCase { public stat
我尝试创建一个搜索器并过滤每个注册用户的结果,但我得到的只是“'User' 类型的值没有成员'lowercased'”作为错误... 错误出现在下一行 let searchedUser = users
我在这里有点困惑,出于某种原因我不能以相同的方式调用 capitalized 和 lowercased,即使两者都应该是实例属性: var aString = "a lowercased string
我目前使用以下代码从数据框中选择行: list = ['3d block','3D Block','3D block','3d Block','cafe','Cafe'] data[data['des
如标题所述:将字符串从任意大小写转换为小写并保持部分不变的最佳方法是什么?例如一个字符串,如:FormatDate(%M)==2 或 stArTDate(%Y/%m)==11/3 我想将其转换为 fo
我想将所有表名都保留为小写。示例 人 我使用 Liquibase设置我的数据库,它看起来像 Add Person Table
我想我在导入 pylab 时遇到了问题。导入 numpy 时会出现类似的错误。这是我的代码 from math import radians, sin, cos from pylab import p
为什么要混合使用小写字母和大驼峰字母? namedtuple deque Counter OrderedDict defaultdict 为什么使用 collections 而不是 Collect
我有一个搜索字符串数组的函数。它工作得很好,除了它区分大小写。我需要它不区分大小写。 最简单的方法是让函数将数组转换为小写,这样当函数在数组中执行搜索时,无论大小写如何,它都能找到它。 我尝试在我的函
如果后续字符不是小写,我如何保留所有大写字符? 考虑这个例子: import re test1 = 'ThisIsATestTHISISATestTHISISATEST' re.findall(r'[
我有以下 text_field: 哪些输出: 手头的问题是:我的属性是 data-date-clearBtn,但 Rails 呈现 data-date-clearbtn(小写 b)。我怎样才能避免
我开始使用 Apache Lucene 8.0。我想知道如何使用 Lucene 将我的 String text 变量转换为小写。我不太确定如何做到这一点,因为我找不到任何例子。我想要的是这样的: pu
我正在尝试用 Atom 编辑器中的大写/小写等效项替换字符串中的某些字符。 假设我有字符串: some:test:sequence 并想要结果: Some:Test:Sequence 我知道诸如 \u
我有点被这个问题困住了。当我运行程序时,由于某种原因,循环经过 z 的所有字母都不会打印。问题来自这个链接:http://docs.cs50.net/2016/x/ap/problems/caesar
我已经搜索过 SO 和 Google,我发现的大多数示例似乎都没有按预期工作(或者没有结合所有这些元素)。我正在尝试创建一个 Regex 表达式,如果字符串包含 至少 字符串中的以下 anywhere
很抱歉这里有很多代码......我尽量让它尽可能短: 我写了(主要是从这里偷来的)一个 HtmlHelper 扩展来写出一个基于枚举的 RadioButton(分组)。我还设置了我的路线图,因此它使用
我是一名优秀的程序员,十分优秀!