0

Hi I have a problem with getting correct ID's and Name when I use ajax-calls to get partials views.

I want ids like "Blocks_0__Items_0__Id" . These are rendered using for loops so Main page:

@for (int blockIndex = 0; blockIndex < Model.Block.Count(); blockIndex++) {

                @Html.EditorFor(model => Model.Blocks[blockIndex], "BlockView")
            }

and then in turn the BlockView does the same list rendering for items using and editorfor. The for loops makes the Ids and name correct.

However if I want to render the Item Template as a partial view all I will get is naturally without the Blocks and Items part.

To solve this issue I call on an empty main page that renders a block with always a single Item in it. but this means that for whatever I want to insert via ajax and readable by the modelbinder I have to create 2 extra partial views!!

Solution 2 I thought was to use the ViewData.TemplateInfo.HtmlFieldPrefix method and set that. But since I have a list withing a list tha tis not possible. I could get rid of one of the partialviews and use: ViewData.TemplateInfo.HtmlFieldPrefix = "Block"

I saw an mvc 1 and 2 Html helper called: Html.BeginCollectionItem("nameofitem") but that does no longer exists.

Any good ideas?

1 Answer 1

0

Try using @Html.TextBox() instead of @Html.EditorFor()

with @Html.TextBox you can pass it a name and a value

@Html.TextBox("WhateverNameYouWant", Model.Blocks[blockIndex])

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.