3

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

enter image description here

Thank you!

12
  • What exception? And why are you passing object instead of int (should be just return View(id);
    – user3559349
    Commented Sep 19, 2016 at 10:24
  • Why are you upcasting id?
    – nbokmans
    Commented Sep 19, 2016 at 10:26
  • 2
    I'm pretty sure (not checked, please correct me if I'm wrong) when you use HiddenFor it gets the property name from the model => model.property. As you're not specifying a property, the property name is null, giving you 'Value cannot be null or empty'
    – fdomn-m
    Commented Sep 19, 2016 at 10:41
  • 2
    The issue is that your model is a value type and you cannot use a value type as the TModel in public static MvcHtmlString HiddenFor<TModel, TProperty>(... You need to wrap the property in a model, or generate the html manually
    – user3559349
    Commented Sep 19, 2016 at 10:54
  • 1
    FYI, the error is throw in the first line of the private static MvcHtmlString InputHelper(... method - refer source code ( at bottom of page). You could write your won extension method to solve it
    – user3559349
    Commented Sep 19, 2016 at 11:05

3 Answers 3

1

Change your hidden field code to it works with object as model

@Html.Hidden("GenericID",Model==null?string.Empty:Model.ToString());
0
0

Thank you @freedomn-m , I think you were right. Anyway I would like to know how to use HiddenFor with Model as Single Object (string/int), because if I use I can't dynamycally set the id right? because it's not an asp control but an html one.

I got it fixed using the following solution (I was trying to avoid a Model Class, because I really just the single int/string id attribute).

CSHTML

@model MyNameSpace.Models.FrontendModel 
@Html.HiddenFor(model => model.categoryId, new { id = "DetailCategoryId" });

CONTROLLER

public ActionResult GetCategoryDetail(int id)
{
    return View(new FrontendModel() { categoryId = id});
}

MODEL

public class FrontendModel
{
    public int categoryId { get; set; }
}

PS: If anyone knows how to use single object (string or int) has model please explain me, I would really appreciate. Thanks!

-1

DarkLink I suppose it is your [GET] Action, so does id have some value when it's executing? I think it should look like this:

public ActionResult GetCategoryDetail(int id = 0)
{
   return View(id);   
}

Then if you do not set id by yourself it'll always will be 0 and you don't have null exception.

2
  • Read question.. I added breakpoint, id has value... thank you
    – TiagoM
    Commented Sep 19, 2016 at 10:43
  • Value of 'p' but do the same without up casting to Object, just return View(id)
    – Marcin
    Commented Sep 19, 2016 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.