0

There is a model in my project

using System;

namespace Argussite.SupplierServices.ViewModels
{
  public class UsersPage
  {
    public Guid SupplierId { get; set; }
    public string SupplierFullName { get; set; }
    public bool ActionsAllowed { get; set; }
  }
}

I use my model in controller and set properties

public ActionResult Index(Guid id)
    {
        var supplierOfUser = Context.Suppliers.AsNoTracking()
            //.Include(e => e.Supplier)
            .FirstOrDefault(e => e.Id == id);

        string SupplierId = id.ToString();
        string SupplierFullName = supplierOfUser.FullName.ToString();
        bool ActionsAllowed = supplierOfUser.Active;

        return View();
    }

and then I need to use that in view, but I don't know how to get my properties in the view?

5 Answers 5

2

Firstly your controller action needs to send that model instance to the view like this:

return View(supplierOfUser);

You need this at the top of your view:

@model Argussite.SupplierServices.ViewModels.UsersPage

Then all you need to do is whereever you want to use your model in the view do:

@Model.SupplierFullName

If you want to use other properties in the view that you can place them into a dynamic ViewBag variable like this:

ViewBag.supplierOfUser = supplierOfUser.FullName.ToString();

and then on your view you can use this value like this:

@ViewBag.supplierOfUser
Sign up to request clarification or add additional context in comments.

Comments

1

Create a new UsersPage and assign the properties you already have to its properties, then pass the newly created UsersPage to the view:

public ActionResult Index(Guid id)
    {
        var supplierOfUser = Context.Suppliers.AsNoTracking()
            //.Include(e => e.Supplier)
            .FirstOrDefault(e => e.Id == id);

        var usersPage = new UsersPage();
        usersPage.SupplierId = id.ToString();
        usersPage.SupplierFullName = supplierOfUser.FullName.ToString();
        usersPage.ActionsAllowed = supplierOfUser.Active;

        return View(usersPage);
    }

Comments

1

In order to use your data from the controller to the view, you need to return the view with your model in parameter:

return View(supplierOfUser);

In your view, you'd also need to define the model:

@model YourModel

Note that you don't need to set your supplier info into other variables (only supplierOfUser is required).

Comments

1

Best way is to establish a strongly typed model in the view:

@model Argussite.SupplierServices.ViewModels.UsersPage

Then, just reference the properties on the model, e.g.

@Html.EditorFor(model => model.SupplierFullName)

Comments

1

It's hard to help you with exact code as I don't know what you want to do on your view, this is a general idea of how you're supposed to pass and use your model in your view

Controller

public ActionResult Index(Guid id)
{
    var supplierOfUser = Context.Suppliers.AsNoTracking()
        .FirstOrDefault(e => e.Id == id);

    var usersPage = new UsersPage
    {
        SupplierId = supplierOfUser.Id,
        SupplierFullName = supplierOfUser.FullName,
        ActionsAllowed = supplierOfUser.Active
    }

    return View(usersPage ); // <-- Send viewmodel to view
}

VIEW

// Declare model
@model Argussite.SupplierServices.ViewModels.UsersPage
...
//Use your model here at will
@Html.DisplayFor(modelItem => item.FullName)

...
@Html.EditorFor(model => model.FullName)
@Html.ValidationMessageFor(model => model.FullName)

1 Comment

Why IEnumerable when he is getting FirstOrDefault?

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.