How to pass value by ViewData to Editor Template by @Html.EditorFor
Asked Answered
H

3

9

In the view strongly typed view against @model System.Tuple<Person, List<Survey>>

I use inside a for-each loop:

  @Html.EditorFor(x => survey.Questions)

to render questions in a `Survey. It works flawless.

Now I would also like to pass additional data to the custom Editor Template. I did:

@Html.EditorFor(x => survey.Questions, new { htmlAttributes = new { PersonId = 1000 } })

and then in the Edtior Template I want to refer to this PersonId and display it.

This is Editor Template I made(shortcut for question purposes):

  @using WebApplication2.Models
    @model   Question
    <div>
        @ViewData["PersonId"]
    </div>

but nothing shows up.

How to properly pass PersonId = 1000 to this EditorTemplate.

Honourable answered 2/9, 2014 at 20:11 Comment(0)
H
21

After some searching around found the following stack-overflow answer for iterating through nested properties. I was unable to get the cast to "dynamic" working, but using reflection correctly retrieved the nested anonymous object. https://mcmap.net/q/914073/-how-to-read-a-property-of-an-anonymous-type

However, if you're determined to use HTML attributes only, you can specify them in a dynamic object:

@Html.EditorFor(x => survey.Questions, new { PersonId = 1000, PersonName = "John", PersonAge=10, etc... })

And access them within the editor using @ViewData:

<div>
     @ViewData["PersonId"]
     @ViewData["PersonName"]
     @ViewData["PersonAge"]
     etc...
</div>
Hachmann answered 27/1, 2015 at 20:39 Comment(1)
Nice. Is it possible to do the same with <editor asp-for= tag helper?Cranwell
Y
2

Try:

@ViewData["htmlAttributes"]["PersonId"]

The outer anonymous object is what populates ViewData. Though, if you use the above, you need to take care that you check that ViewData["htmlAttributes"] actually exists before trying to reference "PersonId" off of it.

Yamada answered 2/9, 2014 at 20:25 Comment(4)
I pasted it and I get syntax error: Error 1 Cannot apply indexing with [] to an expression of type 'object'Honourable
Ok so I made investigation and lot of people have problem with it. It turns out it has to be added to view data by this method @Html.ViewData.Add("PersonId", here I need to instantiate object) before EditorFor. The problem is I have no idead how to pass ANY second parameter there. I would like just to pass int 10 for example.Honourable
Ok I did @{Html.ViewData.Add("PersonId", 100);} there were syntax problems before but I don't know how to display it in the other view.Honourable
To use this method you must pass ViewData as follows: @Html.EditorFor(x => survey.Questions, new { htmlAttributes = new { PersonId = 1000, PersonName = "John", PersonAge=10, etc... }}), and in the view check that ViewData["htmlAttributes"] existsRinehart
P
2

Seems that you haven't set the ViewData. You need to set the ViewData in the controller that returns this View. Just ViewData["PersonId"] = 10

Pargeting answered 2/9, 2014 at 20:51 Comment(5)
Then how to display it in view?Honourable
And why I can't add it in view by:@{Html.ViewData.Add("PersonId", 100);}?Honourable
Have you seen this post? #7032949Pargeting
Not yet. I mostly tried this: codenoevil.com/pass-additional-viewdata-asp-net-mvc-4Honourable
Ok your solution worked but why those @Html helpers did not. They are for some reason there... why they don't work.Honourable

© 2022 - 2024 — McMap. All rights reserved.