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!

TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • What exception? And why are you passing `object` instead of `int` (should be just `return View(id);` –  Sep 19 '16 at 10:24
  • Why are you upcasting `id`? – nbokmans Sep 19 '16 at 10:26
  • I have to use object class to use that view constructor @Stephen Muecke – TiagoM Sep 19 '16 at 10:29
  • Up casting? What do you mean? Int is int32 – TiagoM Sep 19 '16 at 10:30
  • What do you see after runnig you app I mean how page source looks like? Maybe try to use instead HiddenFor - I've some issues with HiddenFor and sometimes best solutions was to use – Marcin Sep 19 '16 at 10:33
  • you are expecting an Int32 but you are sending an Object, I don't think the conversion could be done automatically. see this http://stackoverflow.com/questions/1524197/downcast-and-upcast – Ioana Stoian Sep 19 '16 at 10:34
  • @IoanaStoian thank you, but I already tried before using "@model Object" and still throws the exception, weird stuff – TiagoM Sep 19 '16 at 10:38
  • Don't use `@Html.HiddenFor` and then specify an `id` - just use ` – freedomn-m Sep 19 '16 at 10:39
  • 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' – freedomn-m Sep 19 '16 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(...` You need to wrap the property in a model, or generate the html manually –  Sep 19 '16 at 10:54
  • Thanks @StephenMuecke ! That what I was expecting, thank you so much for your good explanation! So MVC Model only works with custom classes? That's good to know, kinda a limitation imho... – TiagoM Sep 19 '16 at 10:56
  • 1
    FYI, the error is throw in the first line of the `private static MvcHtmlString InputHelper(...` method - refer [source code](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Html/InputExtensions.cs) ( at bottom of page). You could write your won extension method to solve it –  Sep 19 '16 at 11:05

3 Answers3

1

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

@Html.Hidden("GenericID",Model==null?string.Empty:Model.ToString());
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!

TiagoM
  • 3,458
  • 4
  • 42
  • 83
-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.

A_Sk
  • 4,532
  • 3
  • 27
  • 51
Marcin
  • 821
  • 12
  • 20