0

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>
user1476956
  • 61
  • 2
  • 3
  • 10
  • possible duplicate of [ASP.NET MVC - View with multiple models](http://stackoverflow.com/questions/944334/asp-net-mvc-view-with-multiple-models) – Erik Philips Jun 03 '14 at 17:58

1 Answers1

0

create a view model

public class ViewModel{
    public Requirements requirements { get; set; }
    public Supplier supplier { get; set; }
}

then on your controller

List<ViewModel> listvm = new List<ViewModel>();
ViewModel vm = new ViewModel();
vm.requirements.RequiernmentId 
vm.supplier.SupplierID = ...
listvm.Add(vm);

Edit

to send one of the items as a list see my changes above. Then pass your view model to the view

return View(listvm);

and on your view

@model List<OTMS.ViewModel.SupplierAnswerViewModel>
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • and how I will pass that into my view ? – user1476956 Jun 03 '14 at 17:05
  • please check my update ..am getting the following error :The model item passed into the dictionary is of type 'OTMS.ViewModel.SupplierAnswerViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[OTMS.ViewModel.SupplierAnswerViewModel]'. – user1476956 Jun 03 '14 at 18:06
  • that is because you are passing a single model to the view but you have defined a list of that model on the view. if you pass a list or change your view to only a single instance the error will go away – Matt Bodily Jun 03 '14 at 18:21
  • hmm, what I need is list and the user "supplier" will give his answer and be post then ....It work like a form to answer question and then I can check every supplier answer ..How do I pass it as list ? – user1476956 Jun 03 '14 at 18:35
  • I try ..but then you can not use some thing like @Html.DisplayFor(modelItem => item.requirements.RequiernmentName).....it will not show me (RequiernmentName,RequiernmentType)...etc – user1476956 Jun 03 '14 at 19:02
  • it looks like you want a one to one on the requirements and suppliers but a list of them. I have changed my answer to reflect that – Matt Bodily Jun 03 '14 at 19:05
  • no, if you look at my code I am passsing a list to the view and the model on the view is a list. they match so there won't be an error – Matt Bodily Jun 03 '14 at 19:26
  • oky for now at least I can get the view with empty list ..I think I have problem within my controller ..(Check the update controller)....I don't want to create new requirement because I have them in my DB so i only get them using the (var project) .. – user1476956 Jun 03 '14 at 19:58