<blockquote class="quote"><div class="op">Quote:</div>I have a requirement to display data from two different model classes in a single view. Infact there are collection of data for this two models, i.e. the view is displaying multiple row data for the model classes. To show this, I have a viewmodel class which contains two list of models, and in my action method I am passing this viewmodel to the view. For displaying the data there is no issue. However, now the requirement is to edit both the table rows, on which I am having problems. Can you let me know how can I do this?</blockquote>
Quote:here are my code samples - Model 1 : public class VendorDetails { [Key] public int LEADSOURCEID { get; set; } public string VENDORNAME { get; set; } public string LEADSOURCEDESCRIPTION { get; set; } public string LEADSOURCESUBFOLDER { get; set; } public string LEADSOURCEIDENTIFYINGPHRASE { get; set; } } Model 2: public class PhraseInformation { [Key,Column(Order =0)] public int LEADSOURCEID { get; set; } [Key, Column(Order = 1)] public string LEADPHRASE { get; set; } [Key, Column(Order = 2)] public string LEADPHRASESEARCHAFTERPHRASE { get; set; } public string LEADFIELD { get; set; } public int LEADFIELDLENGTH { get; set; } public Boolean LEADSTOPATEOL { get; set; } } ViewModel: public class DetailsAndPhraseInformationViewModel { public List<vendordetails> DetailInformationList { get; set; } public List<phraseinformation> PhraseInformationList { get; set; } } Action Method to display the lists- public ActionResult Details(int? leadSourceId) { DetailsAndPhraseInformationViewModel _viewModel = new DetailsAndPhraseInformationViewModel(); VendorDetailsContext _VendorDetailsContext = new VendorDetailsContext(); _viewModel.DetailInformationList = _VendorDetailsContext.Vendors.Where(VendorDetails => VendorDetails.LEADSOURCEID.ToString().Equals(leadSourceId.ToString())).ToList(); _viewModel.PhraseInformationList = _VendorDetailsContext.Phrases.Where(p => p.LEADSOURCEID.ToString().Equals(leadSourceId.ToString())).ToList(); return View(_viewModel); } Details View - @model AimcoWebPortal.Areas.VendorSetup.Models.DetailsAndPhraseInformationViewModel @{ ViewBag.Title = "Details"; Layout = "~/Views/Shared/_Layout.cshtml"; } <!DOCTYPE html> Details of Vendor Source and Phrases for - @Model.DetailInformationList.Single().VENDORNAME @Html.ActionLink("Edit", "Edit", new { leadSourceId = Model.DetailInformationList.Single().LEADSOURCEID }) | @Html.ActionLink("Delete", "Delete", new { leadSourceId = Model.DetailInformationList.Single().LEADSOURCEID }) | @Html.ActionLink("Back to List", "VendorDetails", new { vendorName = "" }) V E N D O R - S O U R C E @foreach (var details in Model.DetailInformationList) { } SOURCE DESCRIPTION SOURCE SUB FOLDER SOURCE IDENTIFYING PHRASE @details.LEADSOURCEDESCRIPTION @details.LEADSOURCESUBFOLDER @details.LEADSOURCEIDENTIFYINGPHRASE @{ if (Model.PhraseInformationList.Count() == 0) { No phrase information found for the vendor !! } else { L E A D - P H R A S E S @foreach (var phrase in Model.PhraseInformationList) { } LEAD PHRASE LEAD PHRASE SEARCH AFTER PHRASE @phrase.LEADPHRASE @phrase.LEADPHRASESEARCHAFTERPHRASE } } On Edit Click the page needs to redirect to a similar view with editable fields.
<blockquote class="quote"><div class="op">Quote:</div>Below is the action on edit button click - public ActionResult Edit(int? leadSourceId) { DetailsAndPhraseInformationViewModel _viewModel = new DetailsAndPhraseInformationViewModel(); VendorDetailsContext _VendorDetailsContext = new VendorDetailsContext(); _viewModel.DetailInformationList = _VendorDetailsContext.Vendors.Where(VendorDetails => VendorDetails.LEADSOURCEID.ToString().Equals(leadSourceId.ToString())).ToList(); _viewModel.PhraseInformationList = _VendorDetailsContext.Phrases.Where(p => p.LEADSOURCEID.ToString().Equals(leadSourceId.ToString())).ToList(); return View(_viewModel); } Edit View - @model AimcoWebPortal.Areas.VendorSetup.Models.DetailsAndPhraseInformationViewModel @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <!DOCTYPE html> <html> <head> <link href="~/Content/Custom.css" rel="stylesheet" /> </head> <body> @using (Html.BeginForm("Submit", "VendorSetup", FormMethod.Post)) { <div> <h2> Edit Vendor - @Model.DetailInformationList.Single().VENDORNAME </h2> <section> @Html.ActionLink("Delete", "Delete", new { leadSourceId = Model.DetailInformationList.Single().LEADSOURCEID }) | @Html.ActionLink("Back to List", "VendorDetails", new { vendorName = "" }) </section> <hr /> @*<section class="button"> @Html.ActionLink("Submit Changes", "Submit", FormMethod.Post) </section>*@ <input class="button" id="submit" type="submit" value="Submit Changes" /> <h2> V E N D O R - S O U R C E </h2> <table class="table"> <tr> <th> SOURCE DESCRIPTION </th> <th> SOURCE SUB FOLDER </th> <th> SOURCE IDENTIFYING PHRASE </th> </tr> @foreach (var details in Model.DetailInformationList) { <tr> <td> <div contenteditable class="editableTableDetails"> @details.LEADSOURCEDESCRIPTION </div> </td> <td> <div contenteditable class="editableTableDetails"> @details.LEADSOURCESUBFOLDER</div> </td> <td> <div contenteditable class="editableTableDetails"> @details.LEADSOURCEIDENTIFYINGPHRASE</div> </td> </tr> } </table> <hr /> @{ if (Model.PhraseInformationList.Count() == 0) { <span style="font-size:x-large;color:teal;font-family:'Microsoft Sans Serif'"> No phrase information found for the vendor !! </span> } else { <h2> L E A D - P H R A S E S </h2> <table class="table"> <tr> <th> LEAD PHRASE </th> <th> LEAD PHRASE SEARCH AFTER PHRASE </th> </tr> @foreach (var phrase in Model.PhraseInformationList) { <tr> <td> <div contenteditable class="editableTableDetails"> @phrase.LEADPHRASE </div> </td> <td> <div contenteditable class="editableTableDetails"> @phrase.LEADPHRASESEARCHAFTERPHRASE </div> </td> </tr> } </table> } } </div> } </body> </html> Action for Submit - public ActionResult Submit(DetailsAndPhraseInformationViewModel model) { VendorDetailsContext _VendorDetailsContext = new VendorDetailsContext(); if (ModelState.IsValid) { ......... return Content("UPDATED"); } else { return Content("UPDATE FAILED"); } } Now when the controller comes to this Submit method, I found the model is containing the two list DetailInformationList and PhraseInformationList as NULL. I need help to solve this, I am not sure why these two lists are returning as NULL and what is the way to solve this. </blockquote>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)