How to pass two model with related date to view MVC4.
I have class of requirements and supplier have to check within he have the specific requirement or not .
So I need to show in my view the list of requirement in one column and supplier check/answer in other column. So I guess I need to pass the 2 models which they related to each other . How to do that? And then store the supplier answers to specific requirement in the DB.
Here my models
namespace OTMS.Models
{
public class Requiernments
{
[Key]
public int RequiernmentId { get; set; }
public int ID { get; set; }//link to project Name
public string RequiernmentName { get; set; }
public string RequiernmentType { get; set; }
}
namespace OTMS.Models
{
public class Supplier
{
[Key]
public int SupplierId { get; set; }
public int RequiernmentId { get; set; }
public string SupplierName { get; set;
public string Answer{ get; set; }
}
}
Update:
My viewmodel :
namespace OTMS.ViewModel
{
public class SupplierAnswerViewModel
{
public Supplier Sup{ get; set; }
public Requiernments Req { get; set; }
}
}
My controller :
public ProjectContext b = new ProjectContext();
public ActionResult ListRequiermentBySupplier(int ID) //Id of the project
{
var Project = b.RequiernmentEntries.Where(s => s.ID == ID).ToList();//geting all requierment under project
List<SupplierAnswerViewModel> listvm = new List<SupplierAnswerViewModel>();
SupplierAnswerViewModel vm = new SupplierAnswerViewModel();
vm.requirements = new Requiernments();
vm.requirements.RequiernmentId = ID;
vm.supplier = new Supplier();
vm.supplier.SupplierId = 2; //am testing supplier of this id=2 for now
listvm.Add(vm);
return View(listvm); ;
}
My view:
@model IEnumerable<OTMS.ViewModel.SupplierAnswerViewModel>
<table>
<tr>
<th>
RequiernmentName
</th>
<th>
RequiernmentType
</th>
<th>
UserId
</th>
<th>
SupplierName
</th>
<th>
Supplier Answer
</th>
<th></th>
</tr>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.requirements.RequiernmentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.requirements.RequiernmentType)
</td>
<td>
@Html.DisplayFor(modelItem => item.supplier.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.supplier.SupplierName)
</td>
<td>
@Html.DisplayFor(modelItem => item.supplier.Answer)
</td>
<td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
</td>
</tr>
}
}
</table>