Reputation: 815
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)
{
<li>
@using (@Html.BeginForm("Duplicate, "Daily"))
{
<p>@item.AuthorComment</p>
@Html.Hidden("EstadoDeAlmaID", @item.EAID)
@Html.Hidden("PosterID", Session["id"].ToString())
<input type="submit" value="Send" />
}
</li>
}
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.
Upvotes: 2
Views: 13824
Reputation: 334
I have found that when i want to use the view bag data in the HTML Getting back to basics has often worked for me
<input type="hidden" name="Data" id="Data" value="@ViewBag.Data" />
this gave the same result.
Upvotes: 0
Reputation: 86
I had the same problem, found that a simple cast solved my problem.
@Html.Hidden("id", (string) ViewBag.ebook.isbn)
Upvotes: 6
Reputation: 1360
The simple solution for me was to use ViewData instead of ViewBag. ViewBag is just a dynamic wrapper around ViewData anyway.
@Html.Hidden("ReportID", ViewData["ReportID"])
but I don't know if this will help in your case or not since you are creating dynamic items in your foreach loop.
Upvotes: 0
Reputation: 7584
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<EAListItem> //or whatever the type name is
Then just do:
@foreach(var item in Model)
Upvotes: 2
Reputation: 15503
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 <li></li>
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"))
{
<ul>
@foreach (? item in ViewBag.EAList)
{
<li>
<p>@item.AuthorComment</p>
@Html.Hidden("EstadoDeAlmaID", item.EAID)
@Html.Hidden("PosterID", Session["id"].ToString())
<input type="submit" value="Send" />
</li>
}
</ul>
}
Where ?
in the foreach
loop is the type of your items in EAList
.
Upvotes: 4
Reputation: 40149
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.
Upvotes: 1