0

In my Repository class I have this two queries that I want it to appear in just one view:

public ClassXXXX GetxxxoList(string Name)
{
    return context.ClassXXXX.Where(v => v.Name == Name).Single();
}

And the second query in Repository class I have:

public IEnumerable<ClassXXXX> List()
{
    return context.ClassXXXX.ToList();
}

Then in my view I am doing this:

@model IEnumerable<Namespace.Models.ClassXXXX>

@model Namespace.Models.ClassXXXX

To return the two queries in my view respectively. But ASP.NET throws exception on using @model twice in just one view.

3
  • I have no clue what you want or what your doing. Commented Jun 25, 2015 at 14:27
  • create common model, model = new YourModel { SingleObject = context.ClassXXXX.Where(v => v.Name == Name).Single(), MassObject = context.ClassXXXX.ToList() } Commented Jun 25, 2015 at 14:30
  • Hi Igor, tried using your approach but it tells me that SingleObject or MassObject is undefined. The model you mean in this context are you referring to a method to be created in a repository class or an entire new class. Commented Jun 25, 2015 at 14:44

2 Answers 2

1

instead of:

@model IEnumerable<Namespace.Models.ClassXXXX>
@model Namespace.Models.ClassXXXX

You can create an ViewModel class, that contains all your needed data:

public class YourContextViewModel
{
    public List<Person> Person { get; set; }
    public string UserName{get;set;}
    ...
}

It's a good idea to create an ViewModel object to populate your views.

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

1 Comment

This is the proper way. You could also pass it in via ViewData, but this method is the most proper way.
0

I would suggest:

Create a new model that encapsulates both models. Create the model ModelXXXX in the Models namespace

public class ModelXXXX
{
    public ModelXXXX(ClassXXXX singleXXXX, List<SingleXXXX> listXXXX)
    {
        SingleXXXX = singleXXXX;
        ListXXXX = listXXXX;
    }

    public ClassXXXX SingleXXXX { get; set; };
    public IList<ClassXXXX> ListXXXX { get; set; }
}

Then in the controller you would have something like:

public virtual ActionResult Index()     
{
    [...]
    string name = "name";
    ModelXXXX model = new ModelXXXX(GetxxxoList(name), List());

    return View("..Views/Index.cshtml", model)
}

And finally in the view you would have:

@model Namespace.Models.ModelXXXX

[...]
@Model.SingleXXXX 
@Model.ListXXXX

The first one (@Model.SingleXXXX) will have the value of @model Namespace.Models.ClassXXXX

And the second one (@Model.SingleXXXX) will have the value of @model IEnumerable

5 Comments

Hi Petrov, I just have one Model whose name is classXXX. I am using this classXXX to define two queries in my repo class and returning it in my view class
Hi Patrick, I don't think it is possible to use model more than once in a single view. That's why I have suggested using a model to hold both of them. So you might have new ModelXXXX { SingleXXXX = GetxxxoList(name), ListXXXX = List()}; Pass this model to the View, and the view will use Model <path>.ModelXXXX and in the view you can use Model.SingleXXXX and Model.ListXXXX
Hi @PatrickNdu, I have edited my answer to make it more clear, I hope it helps.
I do agree with @SvetoslavPetrov but you also can have model which contains only IList<ClassXXXX> and set ViewBag property inside controller to ClassXXXX from the second method and then use this ViewBag in the View
Thanks Petrov. Well explained.

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.