1

I have this controller:

public ActionResult Index(UserDetails user)
        {
            user.UserEmail = "Email";
            return View();
        }

And this lines

@Html.DisplayNameFor(model => model.UserEmail)
@Html.EditorFor(model => model.UserEmail)

How can I past a value from the controller in the view (in the input box for the UserEmail)?

Thanks for your help Stefan

Stefan
  • 555
  • 5
  • 18

2 Answers2

2

Pass your UserDetails object to the view.

public ActionResult Index(UserDetails user)
{
   user.UserEmail = "Email";
   return View(user);
}

Assuming your razor view (Index.cshtml) is strongly typed to UserDetails

@model UserDetails 
@Html.DisplayNameFor(model => model.UserEmail)
@Html.EditorFor(model => model.UserEmail)
Shyju
  • 214,206
  • 104
  • 411
  • 497
-1

There are multiple ways to pass values from the controller to the view.. I would use viewData and viewBag:

public ActionResult Index(UserDetails user)
    {
        user.UserEmail = "Email";
        ViewData["ArbitraryName"] = user;
        return View();
    }

In your view:

@Html.DisplayNameFor(model => model.UserEmail)
@Html.EditorFor(model => model.UserEmail)
@{ var user = ViewBag.ArbitraryName;}

To access specific data from there, use @user.YourData

See here for more on this specific topic: https://chsakell.com/2013/05/02/4-basic-ways-to-pass-data-from-controller-to-view-in-asp-net-mvc/

Collateral.dmg
  • 128
  • 1
  • 10
  • 1
    The person who asked the question is trying to bind model attributes to a text box. Doing it with ViewData or ViewBag is not recommended as it leads to badly maintainable code. They would be better off using proper models. – juunas Jun 03 '16 at 20:59
  • "trying to bind model attributes to a text box" this was never explicitly stated.. and Shyju's answer already included proper models. Merely offering more options for OP, kid – Collateral.dmg Jun 03 '16 at 21:26