Click here to Skip to main content
15,123,923 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a doubt that how can I pass the value from viewbag to view page. I am taking the value from table and passing to viewbag but in the view page it is not returning the data which I need

var t = db.Names.Where(r => r.ID == id).Select(i=> new { i.ID, i.FirstName });
           ViewBag.name=  t;

in view page

@Html.Editor("name", new { htmlAttributes = new { @class = "form-control" } })


If I need the name as abc it is returning correctly in controller but is not showing in view page. the ID and Name are showing as ID and Name itself but how to pass this value to view page
how can I get the text value in editor field but internally it is storing as int that is the value. can somebody help me ??

What I have tried:

I have seach for the result but didn't get the perfect one
Posted
Updated 17-Apr-17 23:08pm
v3

1 solution

Pass
C#
@ViewBag.name

instead of
C#
"name"

to the editor
   
Comments
ammu11 18-Apr-17 4:44am
   
but when I give the code as you said it shows error that 'HtmlHelper<mapping>' has no applicable method named 'Editor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
why is this ?
NightWizzard 18-Apr-17 4:54am
   
That error usually appears when you forgot the parenthesis behind a method e.g.:
object.toString
instead of
object.toString()
Check if you're using correct C# syntax and maybe have a look at the examples here:
http://www.tutorialsteacher.com/mvc/htmlhelper-editor-editorfor
ammu11 18-Apr-17 5:29am
   
when i give i get false instead of name
NightWizzard 18-Apr-17 10:02am
   
Yes, look at your code: you assign the resut of a LINQ query to t (which contains of 2 fields: ID and Firstname). This NOT(!) a string but you handle it as a string the way you assign it to the Viewbag and how passing it to the editor. To show the Firstname, you should pass only that part of the LINQ result to the viewbag.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900