ASP.net MVC RC2 - 复选框有明确的价值观 [英] ASP.net MVC RC2 - Checkboxes with explicit values

查看:118
本文介绍了ASP.net MVC RC2 - 复选框有明确的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有利用现有的HTML辅助生成复选框具有显式值列表(因为是可能的Beta版)一个简单的方法?

例处于测试阶段(可能MVC的Contrib):

.CheckBox(场,6,新{值= 6})将输出:

 <输入ID =字段1类型=复选框值=6NAME =现场/>
<输入ID =字段2类型=隐藏值=6NAME =现场/>
 

但在RC2我得到:

 <输入ID =字段1类型=复选框值=6NAME =现场/>
<输入类型=隐藏值=假NAME =现场/>
 

  

注:我使用的是自定义的助手扩展   产生独特的HTML ID 和   设置检查HTML属性时,   相应的

除了使用原始的HTML,而不是使用HTML辅助有没有设置复选框的一种方式 有明确的价值观?纵观codePLEX源$ C ​​$下RC2似乎没有成为一个简单的方法为 .InputHelper()标记私人,所以你不能用它来帮助。

注:有多个在页面复选框,允许多选,所以值是没有用的,因为我需要从隐藏字段中的值就知道被选中哪些项目,哪些不是(非检查复选框值不贴 - 只有隐藏字段)。

解决方案

作为一种解决办法,我想出了下面基于松散的RC2源$ C ​​$ C的InputHelper方法,但适应它允许多个值:

私人静态字符串CheckBoxWithValue(这HtmlHelper的HtmlHelper的,字符串名称,对象的值,布尔器isChecked,布尔SETID,IDictionary的htmlAttributes)
    {
    常量InputType inputType = InputType.CheckBox;
    如果(String.IsNullOrEmpty(名))
    {
    抛出新的ArgumentException(空或空的错误,姓名);
    }

    VAR tagBuilder =新TagBuilder(输入);
    tagBuilder.MergeAttributes(htmlAttributes);
    tagBuilder.MergeAttribute(类型,HtmlHelper.GetInputTypeString(inputType));
    tagBuilder.MergeAttribute(姓名,名字,真);
    字符串valueParameter = Convert.ToString(值,CultureInfo.CurrentCulture);

    如果(器isChecked)
    {
    tagBuilder.MergeAttribute(选中,选中);
    }
    tagBuilder.MergeAttribute(价值,valueParameter,真正的);
    如果(SETID)tagBuilder.GenerateId(名称);

    //如果有一个名为领域的任何错误,我们添加的CSS属性。
    ModelState中的ModelState;
    如果(htmlHelper.ViewData.ModelState.TryGetValue(名字,出来的ModelState))
    {
    如果(modelState.Errors.Count> 0)
    {
    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
    }
    }
    VAR inputItemBuilder =新的StringBuilder();
    inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));

    VAR hiddenInput =新TagBuilder(输入);
    hiddenInput.MergeAttributes(htmlAttributes);
    hiddenInput.MergeAttribute(类型,HtmlHelper.GetInputTypeString(InputType.Hidden));
    hiddenInput.MergeAttribute(姓名,名);
    如果(SETID)
    {
    hiddenInput.GenerateId(名称);
    }
    hiddenInput.MergeAttribute(价值,valueParameter);

    inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
    返回inputItemBuilder.ToString();

    }

这依靠的ToString()的评估对象的价值,但足以满足我的场景,我使用整数重新与后端数据库进行查找present选项。注意SETID布尔似乎刚刚替换的Html名点有下划线。当在HTML中使用多个复选框的ID应该是唯一的,所以我只需通过htmlAttributes收集传递的唯一ID代替。

Is there a simple way to use the existing Html Helpers to generate a list of checkboxes with explicit values (as was possible in Beta)?

Example in Beta (possibly with MVC Contrib):

.CheckBox("Field", 6, new { value = 6}) would output:

<input id="Field1" type="checkbox" value="6" name="Field" />
<input id="Field2" type="hidden" value="6" name="Field" />

yet in RC2 I get:

<input id="Field1" type="checkbox" value="6" name="Field" />
<input type="hidden" value="false" name="Field" />

NB: I use a custom helper extension that generates unique HTML ids and sets the checked HTML attribute when appropriate

Besides using raw HTML and not using Html Helpers is there a way of setting checkboxes with explicit values? Looking at the Codeplex source code for RC2 there doesn't appear to be an easy way as .InputHelper() is marked private so you can't use it to help.

NB: There's more than one checkbox in the page to allow multiple selection, so the false value is of no use, as I need the values from the hidden field to know which items were ticked and which weren't (as non-checked checkbox values aren't posted - only the hidden fields are).

解决方案

As a workaround I've come up with the following based loosely on the RC2 sourcecode's InputHelper method, but adapted it to allow multiple values:

private static string CheckBoxWithValue(this HtmlHelper htmlHelper, string name, object value, bool isChecked, bool setId, IDictionary htmlAttributes)
    	{
    		const InputType inputType = InputType.CheckBox;
    		if (String.IsNullOrEmpty(name))
    		{
    			throw new ArgumentException("null or empty error", "name");
    		}

    		var tagBuilder = new TagBuilder("input");
    		tagBuilder.MergeAttributes(htmlAttributes);
    		tagBuilder.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType));
    		tagBuilder.MergeAttribute("name", name, true);
    		string valueParameter = Convert.ToString(value, CultureInfo.CurrentCulture);

    		if (isChecked)
    		{
    			tagBuilder.MergeAttribute("checked", "checked");
    		}
    		tagBuilder.MergeAttribute("value", valueParameter, true);
    		if (setId) tagBuilder.GenerateId(name);

    		// If there are any errors for a named field, we add the css attribute.
    		ModelState modelState;
    		if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState))
    		{
    			if (modelState.Errors.Count > 0)
    			{
    				tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
    			}
    		}
    		var inputItemBuilder = new StringBuilder();
    		inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));

    		var hiddenInput = new TagBuilder("input");
    		hiddenInput.MergeAttributes(htmlAttributes); 
    		hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
    		hiddenInput.MergeAttribute("name", name);
    		if (setId)
    		{
    			hiddenInput.GenerateId(name);
    		}
    		hiddenInput.MergeAttribute("value", valueParameter);

    		inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
    		return inputItemBuilder.ToString();

    	}

This relies on .ToString() your evaluate the object's value, but is adequate for my scenario where i'm using integers to represent options with the lookup done in the backend database. Note the setId boolean seems to just replace dots in the Html name with underscore characters. When using multiple checkboxes in HTML the IDs should be unique so I simply pass in the unique Id via htmlAttributes collection instead.

这篇关于ASP.net MVC RC2 - 复选框有明确的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆