MVC3 and Razor - How to place a dynamic value for hidden field?

To avoid the Extension methods cannot be dynamically dispatched exception, use a model instead of ViewBag so you will not be using dynamic objects (this will avoid all the unnecessary casting in the View and is more in line with MVC style in general).

Up vote 2 down vote favorite share g+ share fb share tw.

I'm a beginner about Razor, and sometimes I get stuck with really simple things. I have this foreach loop: @foreach (dynamic item in ViewBag. EAList) { @using (@Html.

BeginForm("Duplicate, "Daily")) { @item. AuthorComment @Html. Hidden("EstadoDeAlmaID", @item.

EAID) @Html. Hidden("PosterID", Session"id".ToString()) } } This line: @Html. Hidden("EstadoDeAlmaID", @item.

EAID) Doesn't work, and I don't know how to make it work, I tried many ways, without @, with (--), with @(--)... Could someone help me to display the dynamic value in my hidden field? In addition, if someone know about a good Razor samples websites, I would be very thankful. Forms asp.

Net-mvc-3 razor link|improve this question edited Nov 7 '11 at 19:29gdoron12.5k3825 asked Nov 7 '11 at 17:51Rubia Gardini3058 74% accept rate.

Have you tried: @Html. Hidden("EstadoDeAlmaID", item. EAID)?

– gdoron Nov 7 '11 at 17:56 Yes, and I get the error: CS1973: 'System.Web.Mvc. HtmlHelper' has no applicable method named 'Hidden' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched.

Consider casting the dynamic arguments or calling the extension method without the extension method syntax – Rubia Gardini Nov 7 '11 at 18:05 That's your problem, @Gardini. Gdoron is right about how you should be calling it... then see my answer; you are using type dynamic, but Extension methods can't work with that. – Andrew Barber Nov 7 '11 at 18:10.

To avoid the Extension methods cannot be dynamically dispatched exception, use a model instead of ViewBag so you will not be using dynamic objects (this will avoid all the unnecessary casting in the View and is more in line with MVC style in general): In your action when you return the view: return View("ViewName", db.EAList.ToList()); In your view, the first line should be: @model IEnumerable //or whatever the type name is Then just do: @foreach(var item in Model).

When I do this I get the error: CS1579: foreach statement cannot operate on variables of type 'MvcWebRole1.Models. DiaryPosts' because 'MvcWebRole1.Models. DiaryPosts' does not contain a public definition for 'GetEnumerator' – Rubia Gardini Nov 7 '11 at 18:34 Ah sorry, typo on my part in the 2nd code sample.

I have ammended, it should have had IEnumerable or List where EAListItem is your row type... – Paul Tyng Nov 7 '11 at 19:09 I'm very new to MVC3, Razor and EF, I can't get what class should I put inside List item, I have only classes generated by DBContext generator. I don´t know how to work with that in more advanced way. I solved my problem using like this: int EAID = item.

EstadoDeAlmaID; @Html. Hidden("EstadoDeAlmaID", EAID ) I know its stupid but it works. – Rubia Gardini Nov 7 '11 at 19:41 Typically in visual studio you can hover over variables to see their types.

So if you put var model = db.EAList.ToList(); in your controller, then hover over model you would see what you need to put at the top of the view. – Paul Tyng Nov 7 '11 at 20:31.

In Razor, once you are in "C# land", you no longer need to prefix values with @ sign. This should suffice: @Html. Hidden("EstadoDeAlmaID", item.

EAID) Check out Scott Gu's article covering the syntax for more help. Update And I would also move your within your using block, as Razor works better when you wrap HTML blocks inside of a code blocks. Also, your Html.

BeginForm should live outside of your loop. @using (@Html. BeginForm("Duplicate, "Daily")) { @foreach (? item in ViewBag.

EAList) { @item. AuthorComment @Html. Hidden("EstadoDeAlmaID", item.

EAID) @Html. Hidden("PosterID", Session"id".ToString()) } } Where? In the foreach loop is the type of your items in EAList.

I don't think it's a good idea to put a form tag outside of li tags. It's invalid html. Only li tags are legal as direct descendants of ul or ol.

– Andrew Barber Nov 7 '11 at 18:12 Sorry, I was assuming the OP had already included ul/ol somewhere else. But you're right, those should wrap the li tags. – Jerad Rose Nov 7 '11 at 18:14 also, while you are correct about how to make that call... that won't fix the problem.

See my answer for why. – Andrew Barber Nov 7 '11 at 18:14 It didn't work... I still get the error – Rubia Gardini Nov 7 '11 at 18:15 Yeah, as Andrew said, var or dynamic should be the actual type of the elements that make up EAList. We don't know what that is in your case, so we can't give you sample code for that.

– Jerad Rose Nov 7 '11 at 18:18.

You got the error, "Extension methods cannot be dynamically dispatched"... therein lies your trouble. You should declare you loop variable not to be of type dynamic, but of the actual type in the collection. Then remove the @ from the item.

EAID call inside the @Html.Hidden() call.

I changed my loop to @foreach (var item in ViewBag. EAList), and remove @ from item variable, it has the same error. – Rubia Gardini Nov 7 '11 at 18:13 Not var.

The actual type of the collection. You can also probably add ViewBag. EAList as ActualListType to be safe.

– Andrew Barber Nov 7 '11 at 18:15 My call in controller is "ViewBag. EAList = db.EAList.ToList();" what should be my variable type in my View? – Rubia Gardini Nov 7 '11 at 18:18 Should be something like @foreach(YourItemClassName item in ViewBag.

EAList) – Paul Tyng Nov 7 '11 at 18:19 @RubiaGardini we have no idea what the type is of those objects. You need to know that. – Andrew Barber Nov 7 '11 at 18:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions