目前,我在将Enum
值保存到我的数据库时遇到问题,因为我没有正确地从ViewModel
映射到我的Model
,反之亦然。问题在于我尝试Edit
现有的Delivery
。它得到status
,但当我尝试save
时,它save
status
。{/ p>
此行delivery.status = model.Status;
不起作用,我不知道是否应该在Enum
中设置ViewModel
。
我很难过,因为我不知道该怎么做。我仍然是一个初学者,所以我试图围绕这个发展,我能不能提出一个解决方案或指出我正确的方向?
查看型号:
public enum Status
{
Dispatched, Delayed, Delivered
}
public class DeliveryVM
{
public int? ID { get; set; }
public int DriverID { get; set; }
public Status Status { get; set; }
}
控制器:
public ActionResult Edit(int? id)
{
....
Delivery delivery = db.Deliverys.Find(id);
// Initialize the view model
DeliveryVM model = new DeliveryVM()
{
ID = delivery.DeliveryID,
DriverID = delivery.DriverID,
Status = model.Status; // this doesn't work
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(DeliveryVM model)
{
// Get the data model based on the ID of the view model
Delivery delivery = db.Deliverys.Find(model.ID);
// Map the view model properties to the data model
delivery.DriverID = model.DriverID;
delivery.status = model.Status; // this doesn't work
// Mark as modified and save
....
return RedirectToAction("Details", new { id = delivery.DeliveryID });
}
型号:
public enum Status
{
Dispatched, Delayed, Delivered
}
public class Delivery
{
public int DeliveryID { get; set; }
[Display(Name = "Driver")]
public int DriverID { get; set; }
public Status Status { get; set; }
}
查看代码:
<div class="form-group">
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.Status, htmlAttributes: new { @class = "form-control", @id="dropdown" })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</div>
这是我在这一行delivery.status = model.Status;
上遇到的错误:
答案 0 :(得分:1)
如果您希望以最简单的方式使ViewModel Status属性成为字符串或int并映射到控制器中的Status或来自您的数据中,或者在您的数据中选择调用,如果您现在必须这样做。< / p>
如果这不是问题,因为我在您的视图中看不到您在做什么,在您的编辑操作方法中,您无法使用视图模型值更新delivery.Status,无论如何我可以看到..
Delivery delivery = db.Deliverys.Find(model.ID);
// Map the view model properties to the data model
delivery.DriverID = model.DriverID;
delivery.Dispatched_Date = model.Dispatched_Date;
delivery.Dispatched_Time = model.Dispatched_Time;
delivery.Delivered_Date = model.Delivered_Date;
delivery.Delivered_Time = model.Delivered_Time;
delivery.Delayed_Date = model.Delayed_Date;
delivery.Delayed_Time = model.Delayed_Time;
delivery.Comment = model.Comment;
//where is this?
delivery.Status = model.Status;//do a mapping from int or string if you change VM like i suggested
答案 1 :(得分:1)
您已为enums
声明了2个单独的Status
,一个在数据模型的程序集中,另一个在视图模型的程序集中。它们是不同的类型,不能从一个转换为另一个。您可以让视图模型引用数据模型程序集中的enum Status
,也可以使用Enum.TryParse()
方法在它们之间进行映射,例如从GET中的数据模型映射到视图模型方法
HealthHabitat.ViewModels.Status status = HealthHabitat.ViewModels.Status.Dispatched;
Enum.TryParse(delivery.Status.ToString(), out status);
DeliveryVM model = new DeliveryVM()
{
ID = delivery.DeliveryID,
DriverID = delivery.DriverID,
Status = status;
};
然后在POST方法中,反向执行
HealthHabitat.Models.Status status = HealthHabitat.Models.Status.Dispatched;
Enum.TryParse(model.Status.ToString(), out status);
delivery.Status = status;