I am trying to get an integer value from my controller to my view, and then adding that value as a Hidden Field to be used later in my JavaScript client-code.
I am getting an exception while trying to add the hidden field.
Here is the steps to reproduce:
My cshtml page:
@model Int32
@{
ViewBag.Title = "GetGeneric";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section DetailJavascript
{
@Scripts.Render("~/Scripts/Generic.js")
}
@Html.HiddenFor(model => model, new { id = "GenericID" });
<h2>GetGeneric</h2>
And here is my Controller code:
public ActionResult GetCategoryDetail(int id)
{
Object p = id;
return View(p); //I added a breakpoint here and the value of variable 'p' is not null !!
}
EDIT: Exception Details
Thank you!
object
instead ofint
(should be justreturn View(id);
id
?HiddenFor
it gets the property name from themodel => model.property
. As you're not specifying a property, the property name is null, giving you 'Value cannot be null or empty'TModel
inpublic static MvcHtmlString HiddenFor<TModel, TProperty>(...
You need to wrap the property in a model, or generate the html manuallyprivate static MvcHtmlString InputHelper(...
method - refer source code ( at bottom of page). You could write your won extension method to solve it