1

I have been stuck on this problem for awhile now. I am trying to get and display information from the foreign or primary table from another table.

For example, I have a person and pet table.

public class Person
{
    public int id { get; set; }
    // rest of the fields here
}

public class Pet
{
    [DisplayName("Belongs to:")]
    public int person_id { get; set; }
    // Rest of the fields here
}

person_id is a foreign key.

This is my view

 @model SpamValley.Models.Pet

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Pet</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.pet_name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.pet_name)
                @Html.ValidationMessageFor(model => model.pet_name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.pet_type)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.pet_type)
                @Html.ValidationMessageFor(model => model.pet_type)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.person_id, "Person")
            </div>
            <div class="editor-field">
            @if (Model == null || Model.person_id == 0)
            {
                Html.DropDownList("person_id", "Select the person this pet belongs to");
            }
            else
            {
                @Html.DisplayFor(M => M.person_id);
            }
            @Html.ValidationMessageFor(model => model.person_id)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

}

Controller:

[HttpGet]
[DisplayName("Create")]
public ActionResult Create() { return Create_Get(0); }

public ActionResult Create_Get(int p_id)
{
    if (p_id == 0)
    {
        ViewBag.person_id = new SelectList(db.People, "id", "first_name");
        return View();
    }
    else
    {
        // Person Ps = db.People.ToList().Single(Per => Per.id == p_id);
        // ViewBag.person_id = Ps.first_name + " " + Ps.last_name;

        Pet P = new Pet { id = p_id };
        return View(P);
    }
}

Now I know there are few things wrong with the code above but I am more worried about how to display information from the other table. For example: I want to display First Name of the person on Pets.Create View. I would also like to display Pets.Name on Person.Index View.

I can do this easily on SQL database but I am a little confused on mvc logic for this.

Any help is greatly appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
JohnC1
  • 841
  • 1
  • 12
  • 27
  • Doesn't the class Pet has a property called Person that returns the person who it belongs? And the Person class should has a property called Pets that contains all his pets – Esteban Elverdin Oct 04 '13 at 18:21

1 Answers1

0

First, add a collection property to Person to hold all that person's pets, and add a property to Pet to hold that pet's owner.

public class Person
{
    public int id { get; set; }
    // rest of the fields here

    public virtual ICollection<Pet> Pets { get; set; }
}

public class Pet
{
    [DisplayName("Belongs to:")]
    public int person_id { get; set; }
    // Rest of the fields here

    public virtual Person Owner { get; set; }
}

Second, you will probably need to do some minor configuration using Entity Framework's fluent API.

Third, edit your view to take advantage of your new properties. For example, to show the name of a pet's owner:

@Html.DisplayFor(model => model.Owner.Name)
devuxer
  • 41,681
  • 47
  • 180
  • 292
  • Thanks! This solved one of my problems. By the way, I am not too sure of the MVC logic but is this the best way of reading from database? I have this private SpamValleyEntities db = new SpamValleyEntities();, which will connect to database but I feel like it's making a new connection on every request and could be slow. Are there anything like Thread/Connection Pool I could be doing to speed things up or is this how MVC work? – JohnC1 Oct 04 '13 at 19:04
  • @JohnC1, MVC projects I've worked on haven't had a huge number of users, but I can say I've never had an issue with creating a new DbContext for every request. If you search StackOverflow, you should find lots of questions already on that topic (e.g., http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why/10588594#10588594). – devuxer Oct 04 '13 at 19:13