In my controller, I have a ViewModel containing 2 different models, called Drug and Food.

public class FoodDrugViewModel {
        public IEnumerable<SGHealthDesktop.Models.Drug> Drugs { get; set; }
        public IEnumerable<SGHealthDesktop.Models.Food> Foods { get; set; }

    }

In my MainController, this is how I pass in the ViewModel into the Index.

 // GET: Admin
        public ActionResult Index() {
            FoodDrugViewModel vm = new FoodDrugViewModel(); //initialize it
            vm.Drugs = db.Drugs.ToList();
            vm.Foods = db.Foods.ToList();
            return View(vm);
        }

In my view, I created two tables, and looped the items in each of the model, like this.

<table class="table" id="drugTable">
    <thead>
        <tr>
            <th>Drug Name</th>
            <th>Dosage</th>
            <th>Unit</th>
            <th>Type</th>
        </tr>
    </thead>
    @foreach (var item in Model.Drugs) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.DrugName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Dosage)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Type)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.DrugId }) |
                @Html.ActionLink("Details", "Details", new { id = item.DrugId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.DrugId })
            </td>
        </tr>
    }
</table>

 @foreach (var item in Model.Foods) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FoodName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Protein)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Carbohydrate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalFat)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.FoodId }) |
            @Html.ActionLink("Details", "Details", new { id = item.FoodId }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.FoodId })
        </td>
    </tr>
}

To prevent both tables from showing up at the same time, I used a dropdownlist accompanied with JQuery so user can choose which table to see, and it's working as expected. However, my issue are as follow.

When I click on the "Details" ActionLink, or infact any of the 3 ActionLinks (Detail, Edit, Delete), I would like the relevant information to be displayed. For example, if I'm viewing the Drug table and if I click on "Details", the Detail view will display the drug information, and the same for Food.

However, I can't seem to figure out how that can be achieved. My Detail method is as follow, having Drug as the main model still. How can it detect if the user has chose to view the detail of Drug OR Food? As you can see in the code, it immediately finds the Drug details based on the id.

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

As for Create, there's no problem with it as I can again, allow a dropdownlist so user can choose what type, Drug or Food, they want to create and the form will be shown respectively (assuming in view I'm using the FoodDrugViewModel instead of Drug model). But how can i bind the data in the controller? By default, the Create method is as follow.

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "DrugId,DrugName,Dosage,Unit,Type")] Drug drug) {
            if (ModelState.IsValid) {
                db.Drugs.Add(drug);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(drug);
        }

Any help rendered will be much appreciated. Thanks in advance!

Related posts

Recent Viewed