0

Im new to ASP.NET MVC3.

I tried to pass value from controller to a view and then assign that value to text input inside the view.

I tried with viewbag and viewdata. While debugging i could find the value inside viewbag but when my view appers in the browser, textbox is empty.

However , if i assign some value to viewbag from within .cshtml file, it work.

Controller

public ActionResult Contact()
{
 ViewBag.fullname = "Hello";
 ........
 return view();
}

View

@{ ViewBag.lastname = "Hello";
}
@Html.TextBox("fullname", (string)ViewBag.fullname)
@Html.TextBox("lastname", (string)ViewBag.lastname)

output

first textbox - empty

second textbox - Hello

Mridul Raj
  • 1,001
  • 4
  • 19
  • 46
  • to start with using Viewbag, you might want to check [this](http://yassershaikh.com/how-to-pass-values-from-controller-to-view-using-viewbag-in-mvc-3/) out – Yasser Shaikh Aug 08 '12 at 06:22

1 Answers1

5

Try this out...

Controller

public ActionResult Contact()
{
 ViewBag.fullname = "Hello";
 ViewBag.lastname = "World";

 return View();
}

View

@Html.TextBox("fullname", (string)ViewBag.fullname)
@Html.TextBox("lastname", (string)ViewBag.lastname)

Also check this

Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
  • thanks for response dude. This is exactly what i did but its not working for me. Are you sure this works for you? – Mridul Raj Aug 08 '12 at 06:29