0
<div class="firstColumn">
<% for (int rowCount = 0; rowCount < Model.TaskAttributes.Count; rowCount += 2)
   {%>
       <%= Html.EditorFor(model => model.TaskAttributes[rowCount]) %>
 <%}
%>
</div>

So, I have this bit of code which works fine, but I'm unhappy with all the MVC mark-up.

The solution does not support Razor syntax yet, so I'm stuck with this syntax, but I'm wondering if it can be cleaned up at all.

What I was thinking should work is this:

<div class="firstColumn">
<% for (int rowCount = 0; rowCount < Model.TaskAttributes.Count; rowCount += 2)
   {
       Html.EditorFor(model => model.TaskAttributes[rowCount]);
   }
%>
</div>

This does not render any EditorFor's to my page, though. It appears that the '<%=' before the editorFor is cruicial. Is there any way to express this without having to close and open tags?

6
  • even if that code works, it s not a good code. it shouldnt have been doing that in your view. Commented Nov 15, 2012 at 0:37
  • 2
    I am aware of this. I have inherited technical debt and do not have the time scheduled to rewrite and propagate the changes everywhere, but if a small clean-up is possible then I would take that. Commented Nov 15, 2012 at 0:40
  • 1
    You could try @Html.EditorFor(model => model.TaskAttributes[rowCount]);. Commented Nov 15, 2012 at 0:50
  • 1
    <%= is shortcut for Response.Write() . Have you tried that? Commented Nov 15, 2012 at 7:22
  • 1
    @SeanAnderson You need to be very, very careful when using Response.Write. This is a raw dump of information into the response stream. You lose all validation and safety of the output and take total responsibility for it. I don't know that's worth losing a couple WebForms tags... Commented Nov 16, 2012 at 15:04

3 Answers 3

1

That's really about as good as it's going to get. You need to have:

<%= Html.EditorFor(model => model.TaskAttributes[rowCount]) %>

...so that the output is emitted to the response stream. Even with MVC3 syntax you would have to write:

@for (int rowCount = 0; rowCount < Model.TaskAttributes.Count; rowCount += 2) {
    @Html.EditorFor(model => model.TaskAttributes[rowCount])
}

and not just:

@for (int rowCount = 0; rowCount < Model.TaskAttributes.Count; rowCount += 2) {
    Html.EditorFor(model => model.TaskAttributes[rowCount])
}
Sign up to request clarification or add additional context in comments.

Comments

0

<%= is shortcut for Response.Write() . Have you tried that?

Comments

0
<div class="firstColumn">
    @for (int rowCount = 0; rowCount < Model.TaskAttributes.Count; rowCount += 2)
    {
        @Html.EditorFor(model => model.TaskAttributes[rowCount])
    }
</div>

1 Comment

This is a valid MVC3 conversion, but the OP has mentioned that Razor syntax is not available to him. He's just trying to refactor some of the seemingly extraneous script tags.

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.