本文整理汇总了C#中System.Web.Mvc.HtmlHelper.GetModelStateValue方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlHelper.GetModelStateValue方法的具体用法?C# HtmlHelper.GetModelStateValue怎么用?C# HtmlHelper.GetModelStateValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Mvc.HtmlHelper
的用法示例。
在下文中一共展示了HtmlHelper.GetModelStateValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSelectListItems
public static IEnumerable<SelectListItem> GetSelectListItems(HtmlHelper html, ISelectListBuilder dropDownList, string expressionText)
{
string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
bool flag = false;
IEnumerable<SelectListItem> selectList = dropDownList.SelectList();
bool allowMultiple = dropDownList.Attr("multiple") == "multiple";
if (selectList == null)
{
selectList = html.GetSelectListItemsFromViewData(expressionText);
flag = true;
}
object defaultValue = allowMultiple ? html.GetModelStateValue(fullHtmlFieldName, typeof(string[])) : html.GetModelStateValue(fullHtmlFieldName, typeof(string));
if (!flag && defaultValue == null && !String.IsNullOrEmpty(expressionText))
{
defaultValue = html.ViewData.Eval(expressionText);
}
if (defaultValue != null)
{
selectList = GetSelectListWithDefaultValue(selectList, defaultValue, allowMultiple);
}
return selectList;
}
示例2: SelectInternal
private static MvcHtmlString SelectInternal(HtmlHelper htmlHelper, string optionLabel, string name, IEnumerable<SelectListItem> selectList, ModelMetadata metadata, bool allowMultiple, IDictionary<string, object> htmlAttributes)
{
var fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
if (string.IsNullOrEmpty(fullName))
throw new ArgumentNullException("name");
var usedViewData = false;
// If we got a null selectList, try to use ViewData to get the list of items.
if (selectList == null)
{
selectList = htmlHelper.GetSelectData(fullName);
usedViewData = true;
}
object defaultValue = (allowMultiple) ? htmlHelper.GetModelStateValue(fullName, typeof(string[])) : htmlHelper.GetModelStateValue(fullName, typeof(string));
// If we haven't already used ViewData to get the entire list of items then we need to
// use the ViewData-supplied value before using the parameter-supplied value.
if (!usedViewData)
{
if (defaultValue == null)
{
defaultValue = htmlHelper.ViewData.Eval(fullName);
}
}
if (defaultValue != null)
{
var defaultValues = (allowMultiple) ? defaultValue as IEnumerable : new[] { defaultValue };
var values = from object value in defaultValues select Convert.ToString(value, CultureInfo.CurrentCulture);
var selectedValues = new HashSet<string>(values, StringComparer.OrdinalIgnoreCase);
var newSelectList = new List<SelectListItem>();
foreach (var item in selectList)
{
item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
newSelectList.Add(item);
}
selectList = newSelectList;
}
// Convert each ListItem to an <option> tag
var listItemBuilder = new StringBuilder();
// Make optionLabel the first item that gets rendered.
if (optionLabel != null)
{
listItemBuilder.AppendLine(ListItemToOption(new SelectListItem { Text = optionLabel, Value = String.Empty, Selected = false }));
}
foreach (var item in selectList)
{
listItemBuilder.AppendLine(ListItemToOption(item));
}
var tagBuilder = new TagBuilder("select")
{
InnerHtml = listItemBuilder.ToString()
};
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttribute("name", fullName, true /* replaceExisting */);
tagBuilder.GenerateId(fullName);
if (allowMultiple)
{
tagBuilder.MergeAttribute("multiple", "multiple");
}
// If there are any errors for a named field, we add the css attribute.
ModelState modelState;
if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState))
{
if (modelState.Errors.Count > 0)
{
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
}
}
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
}