1

I'm having issues with one of my controller actions .
I have a decimal stored in my viewbag on my view. And am attempting to pass this via and actionlink to my controller method.

ViewBag.Interest =1.25 


@Html.ActionLink("Export", "ExportInterest", "Export", new {id = ViewBag.Interest}, null). 

My controller method looks something like this :

public ActionResult ExportInterest(decimal? id)

{

return View();

}  

I can see the 1.25 be passed via query string but I'm getting a 404 file not found when it's being routed. Note: if I change it to just a whole number with no decimal point it's working fine. Is this an encoding error ? It's not recognising the decimal point , perhaps I need to escape it ? Is there a htmlhelper I should be using? Initially I thought it might be a localisation thing but I have my globalisation culture set up in my web.config. I'm obviously doing something silly here....any help would be appreciated...

Update: I have also tried casting my viewbag to a nullable decimal in the action link but this didn't have any effect

Cœur
  • 37,241
  • 25
  • 195
  • 267
Domino5
  • 43
  • 7
  • Did you have a view created ? Why passing a value from Viewbag ? – Joffrey Kern Jun 25 '13 at 14:31
  • pass as string then cast perhaps –  Jun 25 '13 at 14:35
  • I do have a view. The actionlink and viewbag are populated in view as mentioned above. The viewbag was being populated in the page get but to localise the error I'm now just populating it in a viewbag in the view just to see why a decimal isn't being routed properly correctly.. – Domino5 Jun 25 '13 at 14:39
  • Thanks mehow, I have just trie to pass it as a strig and changed my controller method to expect a string but again the same result. It's definitely an isue with a decimal point in the routing. Would this be a routing or iis issue ? I'm on iis7 btw. But have tried it in iisexpress as well – Domino5 Jun 25 '13 at 14:54

2 Answers2

1

My guess is has to do with the data type in the view bag.

I have passed decimals to controllers before so I know it can be done. But if you changed your link to be:

@Html.ActionLink("Export", "ExportInterest", "Export", new {id =1.25}, null).

Does it work?

taylonr
  • 10,732
  • 5
  • 37
  • 66
  • Thanks for your help taylonr but I already tried this with the same result. Which is why I started to think it wasa culture thing – Domino5 Jun 25 '13 at 14:45
0

Try that:

decimal? d = (decimal?)ViewBag.Interest;

@Html.ActionLink("Export", "ExportInterest", "Export", new {id = d}, null)

or

@Html.ActionLink("Export", "ExportInterest", "Export", new {id = (decimal?)ViewBag.Interest}, null)
  • Hi lukasz , thanks for your help. I already tried the decimal casting with no result .as for passing it into a decimal variable , i can't do that on my view can I ? It would ahve the same result as the decimal casting anyway which didn't work – Domino5 Jun 25 '13 at 14:49
  • sure you can assign variables in View @{ decimal? d = (decimal?)ViewBag.Interest; } – Łukasz Bańcarz Jun 25 '13 at 14:52
  • Thanks for that lukasz that's handy to know. I tried this but I got the same result as the direct casting to decimal? I.e. no change in result. – Domino5 Jun 25 '13 at 14:57
  • look here i think this is issue much like yours: http://stackoverflow.com/questions/5698984/default-asp-net-mvc-3-model-binder-doesnt-bind-decimal-propeties – Łukasz Bańcarz Jun 25 '13 at 15:05
  • Thanks lukasz I'll have a look – Domino5 Jun 25 '13 at 15:20