ASP.NET MVC link between two tables in a single view

Intelli-Seeker
Intelli-Seeker used Ask the Experts™
on
I am using MVC 5 with entity framework version 6 and a SQL server database. I used the database first model to generate the model, controllers, and views.

I have a database with existing data. It has a list of customers and related meetings. One table contains the customers and the other table contains the meetings. One customer can have multiple meetings but a meeting is only associated with a single customer.

I want a link on the Customer details page to create a new meeting based on the ID of the customer in the details view.
 

Here is the code for the customer details view.
@model CRM.Models.Customer

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div id="CustomerDetails">
    <h4>Customer</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.SystemOfRecordId)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.SystemOfRecordId)
        </dd>
        <br />
        @*<dt>
                @Html.DisplayNameFor(model => model.Relationship)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Relationship)
            </dd>*@

        <dt>
            @Html.DisplayNameFor(model => model.Tin)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Tin)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.status_id)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.status_id)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.ConfidenceRating)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ConfidenceRating)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.BankOfficer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.BankOfficer)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.TrustOfficer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.TrustOfficer)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.IsBusinessCustomer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.IsBusinessCustomer)
        </dd>
        <br />
    </dl>

   
    @Html.ActionLink("Create Meeting", "Create", "Meetings", new { id = Model.Id }, null)

    <p />
    <br />

    <table class="table">
        <tr>
            <th>
                Meeting Date
            </th>
            <th>
                Customer
            </th>
            <th>
                Employee
            </th>
            <th>
                Notes
            </th>
            <th>
                Update Meeting
            </th>
        </tr>

        @foreach (var item in Model.Meetings.OrderByDescending(meeting => meeting.InteractionDate))
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.InteractionDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Customer1.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Employee1.FullName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Notes)
                </td>
                <td>
                    @Html.ActionLink("Edit Meeting", "Edit", "Meetings", new { id = item.Id }, null)
                </td>
            </tr>
        }
    </table>
</div>
<p>
    @Html.ActionLink("Edit Customer", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Create New Meeting", "Create", "Meetings", new { id = customer.Id })
    @Html.ActionLink("Back to Home", "Index", "Home")
</p>

Open in new window


Here is the get code for the customers controller

// GET: Customers/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Customer customer = db.Customers.Find(id);
            if (customer == null)
            {
                return HttpNotFound();
            }

            return View(customer);
        }

Open in new window


This is the code for the Meetings Create View

@modelCRM.Models.MeetingModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm("Create", "Meetings", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Meeting</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.InteractionDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InteractionDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InteractionDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.approval_status_id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.approval_status_id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.approval_status_id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerCallDecisionedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CustomerCallDecisionedBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CustomerCallDecisionedBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CustomerCallDecisionDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CustomerCallDecisionDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CustomerCallDecisionDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BankOfficer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BankOfficer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BankOfficer, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TrustOfficer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TrustOfficer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TrustOfficer, "", new { @class = "text-danger" })
            </div>
        </div>

        @*<div class="form-group">
            @Html.LabelFor(model => model.Customer1.Name, "Customer", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Customer", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Customer1.Name, "", new { @class = "text-danger" })
            </div>
        </div>*@

        <div class="form-group">
            @Html.LabelFor(model => model.Employee, "Employee", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Employee", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Employee, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsDeleted, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.IsDeleted)
                    @Html.ValidationMessageFor(model => model.IsDeleted, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfidenceRating, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfidenceRating, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfidenceRating, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsReview, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.IsReview)
                    @Html.ValidationMessageFor(model => model.IsReview, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Attachment", new { @class = "control-label col-md-2"})
            <div class="col-md-10">
                <input type="file" id="attachment" name="upload">
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Open in new window


I attached a screenshot of the model showing the relationships between the Customer table and the Meeting table.

Customer and Meeting table relationships
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

In the view when you create the link i see you use the following:

@Html.ActionLink("Create New Meeting", "Create", "Meetings", new { id = customer.Id })

Open in new window


You are using customer.id. You should use Model.id.

Giannis

Author

Commented:
I updated the view to this code.Updated Details View
I created a meeting using the Create Meeting link in the Customer Details view. When I view the customer details, the meeting does not show in the table. I checked the database and it did not link the meeting to that customer. It created the meeting with a NULL value for Customer in the Meeting table.
Hi again,

Before i continue, does really your Meetings Create View start with that:

@modelCRM.Models.MeetingModel

Open in new window


If it does, then you need a space after @model.

Posting the code of your controller for the Meetings creation would be helpful, but until then i do see another issue. I do not see how you actually link the customer id with the created meeting durring post time.

I would really need to see the code you have in your controller, but i would suggest in your form to add the customer id somehow.

There are a number of ways to do this. You may have a hidden input with the customer id or add it as a part of the post url.

  1. Hidden input :

This case assumes that your CRM.Models.MeetingModel hasd a property like CustomerId that will hold the customer Id.
@using (Html.BeginForm("Create", "Meetings", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
 
    @Html.HiddenFor(x => x.CustomerId)
...
...
}

Open in new window


[list=2]Pass the info using the url of your post :[/list]

@using (Html.BeginForm("Create", "Meetings", new { customerId = x.CustomerId }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
 
    @Html.HiddenFor(x => x.CustomerId)
...
...
}

Open in new window


In this case you need to modify your Post Action to accept the CustomerId as an extra parameter:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create(MeetingModel model, int customerId)
       {
             //find the customer record using the id, and then you can attach the meeting to this record.
       }

Open in new window


Giannis
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Sorry - I pasted it incorrectly in my code snippet. I do have the space in my actual code. I created my project from an existing database and used entity framework 6. The models, views, and controllers were created from the auto-generated code and I have been tweaking the controllers and views. Let me know if you need anything else.

There is still a lot of work to do. If you see something that would work better a different way, please let me know. I am new to the programming world especially in MVC. It is fun and exciting and I am very happy that Experts Exchange has people like you to help me along the way.

Here is the GET and POST code for the MeetingsController.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CRM.Models;
using PagedList;

namespace CRM.Controllers
{
    public class MeetingsController : Controller
    {
        private CRMEntities db = new CRMEntities();

        // GET: Meetings
        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.DateSortParm = String.IsNullOrEmpty(sortOrder) ? "Date_asc" : "";
            ViewBag.NotesSortParm = String.IsNullOrEmpty(sortOrder) ? "Notes_asc" : "";
            ViewBag.ApprovalSortParm = String.IsNullOrEmpty(sortOrder) ? "Approval_asc" : "";
            ViewBag.ascBySortParm = String.IsNullOrEmpty(sortOrder) ? "DecisionBy_asc" : "";
            ViewBag.ascDateSortParm = String.IsNullOrEmpty(sortOrder) ? "DecisionDate_asc" : "";
            ViewBag.BOSortParm = String.IsNullOrEmpty(sortOrder) ? "BO_asc" : "";
            ViewBag.TrOSortParm = String.IsNullOrEmpty(sortOrder) ? "TrO_asc" : "";
            ViewBag.CoRSortParm = String.IsNullOrEmpty(sortOrder) ? "CoR_asc" : "";
            ViewBag.ReviewSortParm = String.IsNullOrEmpty(sortOrder) ? "Review_asc" : "";
            ViewBag.C1SortParm = String.IsNullOrEmpty(sortOrder) ? "C1_asc" : "";
            ViewBag.E1SortParm = String.IsNullOrEmpty(sortOrder) ? "E1_asc" : "";
            
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var Date = from d in db.Meetings
                       select d;
            var Notes = from n in db.Meetings
                        select n;
            var Approval = from a in db.Meetings
                        select a;
            var ascBy = from ascby in db.Meetings
                           select ascby;
            var ascDt = from ascdt in db.Meetings
                         select ascdt;
            var BO = from bo in db.Meetings
                         select bo;
            var TrO = from tro in db.Meetings
                     select tro;
            var CoR = from cor in db.Meetings
                      select cor;
            var Review = from review in db.Meetings
                      select review;
            var C1 = from c1 in db.Meetings
                         select c1;
            var E1 = from e1 in db.Meetings
                     select e1;
            if (!String.IsNullOrEmpty(searchString))
            {
                Notes = Notes.Where(n => n.Notes.Contains(searchString));
                Date = Date.Where(d => d.InteractionDate.Value.Date.Equals(searchString));
                Approval = Approval.Where(n => n.approval_status_id.Equals(searchString));
                ascBy = ascBy.Where(ascby => ascby.CustomerCallDecisionedBy.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "Date_asc":
                    Date = Date.OrderBy(d => d.InteractionDate);
                    break;
                case "Notes_asc":
                    Notes = Notes.OrderBy(n => n.Notes);
                    break;
                case "Approval_asc":
                    Approval = Approval.OrderBy(a => a.approval_status_id);
                    break;
                case "DecisionBy_asc":
                    ascBy = ascBy.OrderBy(ascby => ascby.CustomerCallDecisionedBy);
                    break;
                case "DecisionDate_asc":
                    ascDt = ascDt.OrderBy(ascdt => ascdt.CustomerCallDecisionDate);
                    break;
                case "BO_asc":
                    BO = BO.OrderBy(bo => bo.BankOfficer);
                    break;
                case "TrO_asc":
                    TrO = TrO.OrderBy(tro => tro.TrustOfficer);
                    break;
                case "CoR_asc":
                    CoR = CoR.OrderBy(cor => cor.ConfidenceRating);
                    break;
                case "Review_asc":
                    Review = Review.OrderBy(rev => rev.IsReview);
                    break;
                case "C1_asc":
                    C1 = C1.OrderBy(c1 => c1.Customer1);
                    break;
                case "E1_asc":
                    E1 = E1.OrderBy(e1 => e1.Employee1);
                    break;
                default:
                    Notes = Notes.OrderBy(n => n.Notes);
                    break;
            }

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(Notes.ToPagedList(pageNumber, pageSize));

        }

        // GET: Meetings/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Meeting meeting = db.Meetings.Find(id);
            if (meeting == null)
            {
                return HttpNotFound();
            }
            return View(meeting);
        }

        // GET: Meetings/Create
        public ActionResult Create()
        {
            //ViewBag.Customer = new SelectList(db.Customers, "Id", "SystemOfRecordId");
            ViewBag.Customer = new SelectList(db.Meetings, "Id", "Customer1.Name");
            ViewBag.Employee = new SelectList(db.Employees, "Id", "FirstName");
            return View();
        }

        // POST: Meetings/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,InteractionDate,Notes,approval_status_id,CustomerCallDecisionedBy,CustomerCallDecisionDate,BankOfficer,TrustOfficer,Customer,Employee,IsDeleted,/*ConfidenceRating,*/IsReview")] Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                db.Meetings.Add(meeting);
                db.SaveChanges();
                return RedirectToAction("Details", new { Id = meeting.Id });
            }

            //ViewBag.Customer = new SelectList(db.Customers, "Id", "SystemOfRecordId", meeting.Customer);
            ViewBag.Customer = new SelectList(db.Meetings, "Id", "Customer1.Name", meeting.Customer1);
            ViewBag.Employee = new SelectList(db.Employees, "Id", "FirstName", meeting.Employee);
            return View(meeting);
        }

Open in new window


This is the CustomerModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;


namespace CRM.Models
{
    public class CustomerModel
    {
        [Display (Name = "System of Record ID")]
        public string SystemOfRecordId { get; set; }

        [Display (Name = "TIN")]
        public string Tin { get; set; }

        [Display(Name = "Customer Name")]
        public string Name { get; set; }

        [Display (Name="Prospect Status")]
        public int? status_id { get; set; }
        public IEnumerable <SelectListItem> ProspectStatus { get; set; }
       
        [Display (Name="Confidence Rating")]
        public int? ConfidenceRating { get; set; }
        public IEnumerable<SelectListItem> Confidence { get; set; }
        
        public string BankOfficer { get; set; }

        public string TrustOfficer { get; set; }

        public Boolean IsBusinessCustomer { get; set; }

        public int Id { get; set; }
    }
}

Open in new window


This is the MeetingModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CRM.Models
{
    public class MeetingModel
    {
        public DateTime? InteractionDate { get; set; }
        public string Notes { get; set; }
        public int Customer { get; set; }
        public Int32 approval_status_id { get; set; }
        public string CustomerCallDecisionedBy { get; set; }
        public DateTime? CustomerCallDecisionDate { get; set; }
        public string BankOfficer { get; set; }
        public string TrustOfficer { get; set; }
        public int Employee { get; set; }
        public Int32 ConfidenceRating { get; set; }
        public Boolean IsReview { get; set; }
        public Boolean IsDeleted { get; set; }
        public int Id { get; set; }
        public string Meeting { get; set; }
    }
}

Open in new window

Hi,

I am sorry it took so long to reply, but i got a bit busy. Can you please explain what the problem is now?

Thanks,
Giannis
Hi,

Yes, i see, i had reuested to see the code. So, let's see the "Create" method where you actually creating the meeting record.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,InteractionDate,Notes,approval_status_id,CustomerCallDecisionedBy,CustomerCallDecisionDate,BankOfficer,TrustOfficer,Customer,Employee,IsDeleted,/*ConfidenceRating,*/IsReview")] Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                db.Meetings.Add(meeting);
                db.SaveChanges();
                return RedirectToAction("Details", new { Id = meeting.Id });
            }

            //ViewBag.Customer = new SelectList(db.Customers, "Id", "SystemOfRecordId", meeting.Customer);
            ViewBag.Customer = new SelectList(db.Meetings, "Id", "Customer1.Name", meeting.Customer1);
            ViewBag.Employee = new SelectList(db.Employees, "Id", "FirstName", meeting.Employee);
            return View(meeting);
        }

Open in new window


I see in the above, that you are binding a bunch of properties to your model (by the way, i think you do not need all this, as the ModelBinder knows what to do). But anyway, i see you are expecting a Customer property of the Meeting model. Then, going to your view code i do not see anywhere you passing the customer value, so this field will never be filled in so the saved Meeting will be with no relation to any customer.

As i suggested in a previus post, you should include this as a hidden field. There is no point in requesting the user to re-enter it, as they have come in this view throught the customer details page, so the view already knows the customer id.

So, in the view, where you begin your form try the following:

@using (Html.BeginForm("Create", "Meetings", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    @Html.HiddenFor(x => x.Customer)

@* rest of your code goes here *@

}

Open in new window


This will render the following :

<input type="hidden" name="Customer" value="1" />

Open in new window


Of course the value will not be 1, but rather the Customer's id.

Try this out and let me know how it goes.

Giannis

Author

Commented:
Thanks for your assistance. You have been very helpful thus far. I changed the Meetings/Create view to this:

@using (Html.BeginForm("Create", "Meetings", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
 
    @Html.AntiForgeryToken()

    //added below code from ExpertsExchange Post  - https://www.experts-exchange.com/questions/29124927/ASP-NET-MVC-link-between-two-tables-in-a-single-view.html

    @Html.HiddenFor(x => x.Customer)

    //end added code

    <div class="form-horizontal">
        <h4>Meeting</h4>
@* rest of your code follows below *@

Open in new window


Unfortunately, it did not assign the meeting to the customer. In this case, I created a new customer. I then created a meeting from the Create Meeting link on the details view.

Details view for new customer
New Meeting View redirected from Customer Details create meeting link
When I view the customer, there are no meetings in the details view. I checked the database, and it assigned a null value to the Customer for the meeting that was created.

This is the customer table entry for the new customer:
 Customer Table - new customer from Customer/Create
This is the meeting table entry for the new meeting that was created from the "Create Meeting" Link in the Customer Details view:

Meeting Table - new entry after create meeting link
Could this be an issue with the way the Customer Details view is written? I have the code of the Customer Details below. I created a link on that page to create the meeting. Maybe that is where the problem is?

@model CRM.Models.Customer

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div id="CustomerDetails">
    <h4>Customer</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.SystemOfRecordId)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.SystemOfRecordId)
        </dd>
        <br />
        @*<dt>
                @Html.DisplayNameFor(model => model.Relationship)
            </dt>

            <dd>
                @Html.DisplayFor(model => model.Relationship)
            </dd>*@

        <dt>
            @Html.DisplayNameFor(model => model.Tin)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Tin)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.status_id)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.status_id)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.ConfidenceRating)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ConfidenceRating)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.BankOfficer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.BankOfficer)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.TrustOfficer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.TrustOfficer)
        </dd>
        <br />
        <dt>
            @Html.DisplayNameFor(model => model.IsBusinessCustomer)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.IsBusinessCustomer)
        </dd>
        <br />
    </dl>

   
    @Html.ActionLink("Create Meeting", "Create", "Meetings", new { id = Model.Id }, null)

    <p />
    <br />

    <table class="table">
        <tr>
            <th>
                Meeting Date
            </th>
            <th>
                Customer
            </th>
            <th>
                Employee
            </th>
            <th>
                Notes
            </th>
            <th>
                Update Meeting
            </th>
        </tr>

        @foreach (var item in Model.Meetings.OrderByDescending(meeting => meeting.InteractionDate))
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.InteractionDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Customer1.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Employee1.FullName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Notes)
                </td>
                <td>
                    @Html.ActionLink("Edit Meeting", "Edit", "Meetings", new { id = item.Id }, null)
                </td>
            </tr>
        }
    </table>
</div>
<div>
    @*jQuery script below creates a print contents of a div. Give the div tag an id*@
    <p>
        <script>
            function printContent(el) {
                var restorepage = $('body').html();
                var printcontent = $('#' + el).clone();
                $('body').empty().html(printcontent);
                window.print();
                $('body').html(restorepage);
            }
        </script>
        @*The id of the Div to be printed is inside of the parantheses*@
        <button id="print" onclick="printContent('CustomerDetails');">Print</button>
    </p>
    <br />
</div>
<p>
    @Html.ActionLink("Edit Customer", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Back to Home", "Index", "Home")
</p>

Open in new window

I had a Skype call with Ioannis Paraskevopoulos and he solved my problem. The main issue is that the model was not being passed to the view. This, of course, is not the best way to handle coding, but it worked for me. Hopefully it will lead someone else in the right direction. I updated the Meetings/Create in the Meetings Controller to the following:

// GET: Meetings/Create
        public ActionResult Create(int customerId)
            
        {
            var model = new MeetingModel { Customer = customerId };
            //ViewBag.Customer = new SelectList(db.Customers, "Id", "SystemOfRecordId");
            ViewBag.Customer = new SelectList(db.Meetings, "Id", "Customer1.Name");
            ViewBag.Employee = new SelectList(db.Employees, "Id", "FirstName");
            return View(model);
        }

Open in new window


Then the Create Meeting link was changed to the variable that was created in the controller. Customerid = Model.Id.

   
    @Html.ActionLink("Create Meeting", "Create", "Meetings", new { Customerid = Model.Id }, null)

    <p />
    <br />

    <table class="table">

Open in new window

Author

Commented:
How do I close it and give Ioannis Paraskevopoulos the credit?

Author

Commented:
Thanks a million for assisting with the solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial