Reputation: 55444
When I try to add @Html.HiddenFor(@Model.ID)
to my code, I get the following error when access the page:
Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.InputExtensions.HiddenFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried reading MSDN, but the documentation is awful (they don't provide a single code example in the documentation for this method.
Here is my View:
@model CustomerService.Entity.Order
@using CustomerService.Entity
@{
ViewBag.Title = "OrderDetails";
}
<h2>
OrderDetails</h2>
@using (Html.BeginForm("HandleSubmit", "Home", FormMethod.Post))
{
<table border="1">
<tr>
<td>
<b>Order #</b>
</td>
<td>
@Model.ID
</td>
</tr>
<tr>
<td>
<b>Description</b>
</td>
<td>
@Model.Description
</td>
</tr>
<tr>
<td>
<b>Salesperson Name</b>
</td>
<td>
@Model.SalespersonName
</td>
</tr>
</table>
<h3>
Line Items</h3>
<input id="btnAddLineItem" type="submit" name="AddLineItem" value="AddLineItem" />
@Html.HiddenFor(@Model.ID)
<table border="1">
<tr>
<td>
<b>Line Item ID</b>
</td>
<td>
<b>Description</b>
</td>
</tr>
@for (int i = 0; i < @Model.LineItems.Count; ++i)
{
<tr>
<td>
@Model.LineItems[i].ID
</td>
<td>
@Model.LineItems[i].Description
</td>
</tr>
}</table>
}
Upvotes: 2
Views: 5830
Reputation: 150253
HiddenFor
method should get an Expression
as a parameter not a value:
@Html.HiddenFor(m => m.ID)
Instead of: @Html.HiddenFor(@Model.ID)
Method signature:
HiddenFor<TModel, TProperty>(HtmlHelper<TModel>,
Expression<Func<TModel, TProperty>>)
In plain text you should give an Expression
that gets an "instance" of the type of your model(in this case CustomerService.Entity.Order
) and returns the desired property(in this case ID
)
Upvotes: 4
Reputation: 532465
HiddenFor
takes an expression.
@Html.HiddenFor( model => model.ID )
Upvotes: 6