Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:35:19+00:00 2026-06-16T19:35:19+00:00

Im trying to learn myself C# MVC4 with visual studio 2010. What i want

  • 0

Im trying to learn myself C# MVC4 with visual studio 2010.

What i want to do is get the full output of 2 models on 1 view selected on someones username.

So if i go to /Teams/Details/1
I will get all the info from the models players and teams.

Models:

    public class Players
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int price { get; set; }
    public string coach { get; set; }
    public string Team { get; set; }
}

    public class Teams
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string coach { get; set; }
}

Controller

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using voetbal.Models;

namespace voetbal.Controllers
{
    public class TeamsController : Controller
    {
        private TeamsDBContext db = new TeamsDBContext();

        //
        // GET: /Teams/

        public ActionResult Index()
        {
            return View(db.Teams.ToList());
        }

        //
        // GET: /Teams/Details/5

        public ActionResult Details(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // GET: /Teams/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Teams/Create

        [HttpPost]
        public ActionResult Create(Teams teams)
        {
            if (ModelState.IsValid)
            {
                teams.coach = User.Identity.Name;
                db.Teams.Add(teams);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(teams);
        }

        //
        // GET: /Teams/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // POST: /Teams/Edit/5

        [HttpPost]
        public ActionResult Edit(Teams teams)
        {
            if (ModelState.IsValid)
            {
                db.Entry(teams).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(teams);
        }

        //
        // GET: /Teams/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // POST: /Teams/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Teams teams = db.Teams.Find(id);
            db.Teams.Remove(teams);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

I have absolutely how to get this done, i have tried a viewmodel class but i have no idea how to get this information on someones coach name.

Im looking forward to your reply.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:35:20+00:00Added an answer on June 16, 2026 at 7:35 pm

    There are a lot of ways to skin this cat, but one thing you have to first see is that you’re not sending back even one instance of both models. If you examine the code for the Details action:

    public ActionResult Details(int id = 0)
    {
        Teams teams = db.Teams.Find(id);
        if (teams == null)
        {
            return HttpNotFound();
        }
        return View(teams);
    }
    

    clearly all we are sending back is one instance of the Teams model. So, let’s examine an option that we have. Consider the following code:

    public ActionResult Details(int id = 0)
    {
        Teams teams = db.Teams.Find(id);
        if (teams == null)
        {
            return HttpNotFound();
        }
    
        // get the list of players into a List<Players>
        var players ...
    
        var model = new Tuple<Teams, List<Players>>(teams, players);
    
        return View(model);
    }
    

    now in the View we can set our model to this (note you will need to fully qualify Teams and Players in the @model declaration):

    @model Tuple<Teams, List<Players>>
    

    and from there we can do stuff like:

    Model.Item1.Name
    

    or:

    Model.Item2[0].Name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In trying to learn JavaScript closures, I've confused myself a bit. From what I've
I'm trying to build a Chinese flashcards program in Java to help myself learn
I've been trying to learn this myself several hours and just had to give
I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project. The
I'm trying to learn Serbian atm and got myself a csv file with the
I'm trying to learn to write a WordPress plugin by setting myself a goal
I'm trying to learn regular expressions. I was testing myself by trying to check
Trying to learn myself PHP with some database interaction and I am trying to
I am trying to learn C++ myself by reading a book and do exercise
I'm trying to learn programming by myself, I'm working from a book that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.