1

really new to MVC and not sure how i would get my variable 'UsersFullName' from the code behind class file to the view. With aspx and aspx.cs i would have done this:

index.aspx.cs:

using System.DirectoryServices.AccountManagement;

namespace TeamRequestForm.Controllers
{
    public class AddTeamController : Controller
    {
        // GET: AddTeam
        public ActionResult Index()
        {
            return View();
        }
        private string usersFullName = UserPrincipal.Current.DisplayName;
        public string UsersFullName { get { return usersFullName; } }
    }
}

index.aspx:

<input type="text" value="<%=UsersFullName%>" name="changeOriginator" id="changeOriginator">

How can I do the same in a view? Many thanks in advance.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
Shaheen K
  • 124
  • 1
  • 7

4 Answers4

1

Create a Model:

public class UserModel 
{
    public string FullName { get; set; } 
    // Any other properties
}

And return it to your view:

public ActionResult Index() 
{
    var viewModel = new UserModel { FullName = // Logic for set };
    return View(viewModel);
}

And then in your view, declare this at the top of your view:

@model namespace.UserModel

And in the input you can reference this model by using @model.FullName or, even better:

@Html.EditorFor(model => model.FullName)
ediblecode
  • 11,701
  • 19
  • 68
  • 116
  • Thanks for your reply, when creating var 'viewModel' the Fullname bit is erroring saying it cannot be assigned as its readonly... here is my code so far: public ActionResult Index() { var viewModel = new UserModel { FullName = }; return View(viewModel); } public class UserModel { public string FullName { get { return UserPrincipal.Current.DisplayName; } } // Any other properties } – Shaheen K Oct 20 '16 at 10:51
  • @ShaheenK that's because you're trying to set it, and aren't actually setting it. Because you don't have a setter you can just have `viewModel = new UserModel()` – ediblecode Oct 20 '16 at 11:20
1

Viewbag is one way to do it. Adding it to your model is another. There are more options. Have a look at these articles

http://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/

http://www.webdevelopmenthelp.net/2014/06/asp-net-mvc-pass-data-controller-view.html

Jonny
  • 1,037
  • 7
  • 15
0
        public ActionResult Index()
        {
ViewBag.UsersFullName = UsersFullName;

            return View();
        }

View

@Html.TextBoxFor(c => c.UsersFullName )

check out the MVC documentation on StackOverflow

Emil
  • 281
  • 1
  • 2
  • 11
0

You can do this from many ways, but these are most popular approaches:

1) using ViewBag

public ActionResult Index()
    {
        private string usersFullName = UserPrincipal.Current.DisplayName;
        ViewBag.userFullName=usersFullName;
        return View();
    }

and call this ViewBag at your .cshtml page

like below:

<span>@ViewBag.userFullName</span>

2) using ModelView

For ViewModel approach you have to create Properties for your Entities in class like below:

public class UserViewModel
{
public string userFullName{get; set;}
} 

and use this ViewModel into your Controller like this

public ActionResult Index()
    {
        private string usersFullName = UserPrincipal.Current.DisplayName;
        UserViewModel objCls=new UserViewModel();
        objCls.userFullName=usersFullName;
        return View(objCls);
    }

and use on .cshtml page like below:

@model UserViewModel //your class refrence

<span>@Model.userFullName</span> // you will get your value here

Hope it will helps you. Thanks

Community
  • 1
  • 1
Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33