我正在将ViewModel的值放入表单字段的视图中。在我的一个DropDownList中,值是正确的,但在另一个中,该值会重复自身而不是更改。我在做什么错了?
ViewModel:
namespace FulfillmentPortal.ViewModels
{
public class ViewModel
{
[Required(ErrorMessage = "Please select a carrier")]
public List<Carrier> CarrierList { get; set; }
[Required(ErrorMessage = "Please select a service")]
public List<CarrierService> ServiceList { get; set; }
}
}
控制器:
public class FulfillmentController : Controller
{
private CarrierModel db = new CarrierModel();
// GET: Fulfillment
public ActionResult Index()
{
ViewModel vm = new ViewModel
{
CarrierList = db.Carriers.ToList(),
ServiceList = db.CarrierServices.ToList()
};
return View(vm);
}
}
查看:
@model FulfillmentPortal.ViewModels.ViewModel
@{
ViewBag.Title = "index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="panel panel-primary">
<div class="panel-heading">REPORT OPTIONS</div>
<div class="panel-body" style="padding-left:35px;">
<form id="processForm" class="form-horizontal" action="~/Fulfillment/Report" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sel1">Carrier:</label>
@Html.DropDownListFor(model => model.CarrierList, new SelectList(Model.CarrierList, "CarrierId", "CarrierName"), "Select a Carrier",
new { @class = "form-control", @style = "width:auto; margin-bottom:15px;" })
<label for="sel2">Carrier Services:</label>
@Html.DropDownListFor(model => model.ServiceList, new SelectList(Model.ServiceList, "Code", "WebName"), "Select a Service",
new { @class = "form-control", @style = "width:auto; margin-bottom:15px;" })
</div>
</form>
</div>
</div>
型号:
public partial class CarrierModel : DbContext
{
public CarrierModel()
: base("name=CarrierModel")
{
}
public virtual DbSet<Carrier> Carriers { get; set; }
public virtual DbSet<CarrierService> CarrierServices { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
[HttpPost]
public ActionResult Index(ViewModel viewModel)
{
if (ModelState.IsValid)
{
viewModel.CarrierList.ToList();
viewModel.ServiceList.ToList();
}
return View(viewModel);
}
这是我的控制器。现在我的观点是错误的。我想我缺少了一些东西或误解了一些东西。请帮助我了解我所缺少的。
任何帮助都会很棒。谢谢!
答案 0 :(得分:2)
DropDownListFor(model => model.ServiceList
这不是应该使用这种方法的方式,我怀疑这是您问题的答案。
该lambda应该提供一个字段,该字段将保留此下拉列表输出的值。对于您的情况,您应该在模型中有两个字段:
public class ViewModel
...
public int CarrierId { get; set; }
public string CarrierServiceCode { get; set; }
这些将保留当前选择的值,如果未选择(尚未),则不保存任何值。并且应该在该lambda中使用它们:
DropDownListFor(model => model.CarrierServiceCode
或者,您可以使用DropDownList()
方法,该方法不需要模型中的字段,并为其指定一个自定义名称,该名称将随选定的值一起发布。
答案 1 :(得分:1)
您的ViewModel
应该如下:
public class ViewModel
{
[Required(ErrorMessage = "Please select a carrier")]
public int CarrierId {get; set;}
[Required(ErrorMessage = "Please select a service")]
public int ServiceCode {get; set;}
public List<Carrier> CarrierList { get; set; }
public List<CarrierService> ServiceList { get; set; }
}
然后在视图中:
<label for="sel1">Carrier:</label>
@Html.DropDownListFor(model => model.CarrierId, new SelectList(Model.CarrierList, "CarrierId", "CarrierName"), "Select a Carrier",
new { @class = "form-control", @style = "width:auto; margin-bottom:15px;" })
<label for="sel2">Carrier Services:</label>
@Html.DropDownListFor(model => model.ServiceCode, new SelectList(Model.ServiceList, "Code", "WebName"), "Select a Service",
new { @class = "form-control", @style = "width:auto; margin-bottom:15px;" })
然后,您的Index
Post
方法应如下:
[HttpPost]
public ActionResult Index(ViewModel viewModel)
{
if(ModelState.IsValid)
{
// do whatever you want with `viewModel.CarrierId` and `viewModel.ServiceCode` here
}
viewModel.CarrierList = db.Carriers.ToList();
viewModel.ServiceList = db.CarrierServices.ToList();
return View(viewModel);
}