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

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry i poste it as an answer
-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/

2 Comments

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.
"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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.