troubleshooting Question

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

Avatar of Intelli-Seeker
Intelli-SeekerFlag for United States of America asked on
Microsoft SQL ServerASP.NET.NET MVCSQL* Razor
10 Comments1 Solution239 ViewsLast Modified:
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>

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);
        }

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")
}

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

Customer and Meeting table relationships
ASKER CERTIFIED SOLUTION
Intelli-Seeker

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros