这是我的Controller的Index GET方法:
public async Task<ActionResult> Index()
{
var models = new Tuple<List<Order>, List<Operator>>(await _context.Orders.AsNoTracking().Where(x => x.State == orderState).ToListAsync(), _liveData.Operators);
return View(models);
}
在相关视图中我显示了所有行,并且它们有一个下拉列表:
@model Tuple<List<Order>, List<Operators>>
@using (Html.BeginForm("Index", "Orders", FormMethod.Post, new { @id = "form1" }))
{
@for (var i = 0; i < Model.Item1.Count; i++)
{
@Html.HiddenFor(modelItem => Model.Item1[i].Id)
@Html.DisplayFor(modelItem => Model.Item1[i].Number)
@Html.DisplayFor(modelItem => Model.Item1[i].Quantity)
<select class="custom-select" required>
<option value="None" selected>Select one</option>
@foreach (var op in Model.Item2)
{
<option value="@op.Name">@op.Name</option>
}
</select>
}
}
这里我的不工作的邮政处理程序:
[HttpPost]
public async Task<ActionResult> Index(List<Order> orders, List<Operator> op)
{
// both orders and op are null
}
我期待传递给视图的模型相同,但它们都是空的。
元组语法中是否有错误?
如果这可能会改变某些内容,我在这里添加如何在控制器中定义Order和Operator:
public class OrdersController : Controller
{
public MyContext _context { get; set; }
public LiveData _liveData { get; set; }
public OrdersController(MyContext context, LiveData liveData)
{
_context = context;
_liveData = liveData;
}
}
在DAL中:
public class MyContext : DbContext
{
public IConnectionManager ConnectionManager { get; set; }
public MyContext() : base("MyContext") { }
public DbSet<Order> Orders { get; set; }
}
public class LiveData : IDisposable
{
public MyContext _context { get; set; }
public LiveData(MyContext context)
{
_context = context;
Operators = new List<Operator>();
}
public List<Operator> Operators { get; set; }
}
最后,我使用Autofac注入它们:
public class AutofacContainer
{
public IContainer Container { get; set; }
public AutofacContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<MyContext>().PropertiesAutowired();
builder.RegisterType<LiveData>().PropertiesAutowired().InstancePerLifetimeScope();
Container = builder.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(Container);
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(Container));
}
}
答案 0 :(得分:1)
您的观点是Tuple<List<T>,List<U>>
的强类型,而在您的操作方法中,您指定了两个List<T>
和List<U>
参数,for post将绑定到实际模型,因此您应该将您的操作方法更改为与您的视图强类型的输入参数相同的类型:
[HttpPost]
public async Task<ActionResult> Index(Tuple<List<Order>,List<Operator>> model)
{
// do something here using model
}
但由于Tuple
不包含无参数构造函数,因此上述内容仍无法正常工作,因此框架将无法在post methid中绑定模型。
您可以通过创建一个 ViewModel 来实现这一目标,该 ViewModel 可以保存两个集合:
public class OrderOperatorViewModel
{
public List<Order> Orders {get;set;}
public List<Operator> Operators {get;set;}
}
然后在行动中填充它并返回:
public async Task<ActionResult> Index()
{
var models = new OrderOperatorViewModel();
// fetch data and set properties here
return View(models);
}
然后在视图中使用它:
@model OrderOperatorViewModel
现在正在行动:
[HttpPost]
public async Task<ActionResult> Index(OrderOperatorViewModel model)
{
// todo here
}