如何传递具有相关日期的两个模型来查看MVC4。
我有一类要求,供应商必须检查他是否有特定要求。
所以我需要在我的视图中显示一列中的需求列表和其他列中的供应商检查/答案。所以我想我需要传递它们相互关联的2个模型。怎么做?然后将供应商的答案存储在DB中的特定需求中。
这是我的模特
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; }
}
}
更新:
我的观点模型:
namespace OTMS.ViewModel
{
public class SupplierAnswerViewModel
{
public Supplier Sup{ get; set; }
public Requiernments Req { get; set; }
}
}
我的控制器:
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); ;
}
我的观点:
@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>
答案 0 :(得分:0)
创建视图模型
public class ViewModel{
public Requirements requirements { get; set; }
public Supplier supplier { get; set; }
}
然后在您的控制器上
List<ViewModel> listvm = new List<ViewModel>();
ViewModel vm = new ViewModel();
vm.requirements.RequiernmentId
vm.supplier.SupplierID = ...
listvm.Add(vm);
修改
将其中一个项目作为列表发送,请参阅上面的更改。然后将视图模型传递给视图
return View(listvm);
并在您的观点
@model List<OTMS.ViewModel.SupplierAnswerViewModel>