3
public class Foo { public bool Checked {get;set;}}

View:

<viewdata model="Foo[] fooList" />
<for each="var f in fooList">
    ${Html.CheckBoxFor(x=>x[fIndex].Checked)}
</for>

Will output:

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />

Problem is that System.Web.Mvc.ExpressionHelper.GetExpressionText does not include index in id/name.

That leads to problems in case I want to add a label for every checkbox (because all id`s are the same).

Any ideas how to handle this properly?


From the MVC source=>

 while (part != null) {
            if (part.NodeType == System.Linq.Expressions.ExpressionType.MemberAccess) {
                MemberExpression memberExpressionPart = (MemberExpression)part;
                nameParts.Push(memberExpressionPart.Member.Name);
                part = memberExpressionPart.Expression;
            }
            else {
                //arghhhh... [index] != MemberAccess :(
                break;
            }
        }

2 Answers 2

1

The ability of the expression-based helpers to understand indexes isn't in the product yet. It will be in the next preview release (whatever comes after MVC 2 RC). See http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=4970.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the CheckBoxFor overload that allows you to specify html attributes:

CheckBoxFor(TModel)(HtmlHelper(TModel), Expression(Func(TModel, Boolean)), IDictionary(String, Object))

For example,

${Html.CheckBoxFor(x => x[fIndex].Checked, new { id = "foo" + fIndex) })}

1 Comment

That's not properly. In this case - let me ask what's the benefit of CheckBoxFor 'strongly-typed' helper method if i won't be able to get that id back from view model without messing around with hard-coded strings? That's the same as to use ${Html.CheckBox("foo"+fIndex,m.AllCatalogs[i].Checked)} which even looks better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.