Skip to content
Loading
Pass viewmodel (contain List<ModelClass>) data to controller
On Edit Click the page needs to redirect to a similar view with editable fields.
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";
}
@using (Html.BeginForm("Submit", "VendorSetup", FormMethod.Post))
{

Edit Vendor - @Model.DetailInformationList.Single().VENDORNAME
@Html.ActionLink("Delete", "Delete", new { leadSourceId = Model.DetailInformationList.Single().LEADSOURCEID }) |
@Html.ActionLink("Back to List", "VendorDetails", new { vendorName = "" })

@*
@Html.ActionLink("Submit Changes", "Submit", FormMethod.Post)
*@

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
}
}
}
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.
 
  • Jignesh Kumar
    Could you please share your controller action method for retrive data from db  while click on edit and view for save ?