我先使用实体框架代码。每当我创建数据时,它就会显示一个空列表。虽然没有错误。
我已经删除了行并再次进行了迁移。我已经更新了数据库。并没有将数据保存在本地数据库中。
我的映射配置文件代码如下。
public class MappingProfile: Profile
{
public MappingProfile()
{
Mapper.CreateMap<DonorCreationModels, Donor>();
Mapper.CreateMap<Donor, Donor>();
Mapper.CreateMap<ReceiverCreationModel, Receiver>();
Mapper.CreateMap<Receiver, Receiver>();
}
}
接收器型号如下所示。
public class Receiver
{
[Key]
public int ReceiverId { get; set; }
public string ReceiverName { get; set; }
public int ReceiverPhoneNumber { get; set; }
public string ReceiverAddress { get; set; }
public DateTime LastTimeReceived { get; set; }
public string BloodTypeReceived { get; set; }
}
捐赠者创建模型代码如下所示。
public class ReceiverCreationModel
{
public string ReceiverName { get; set; }
public int ReceiverPhoneNumber { get; set; }
public string ReceiverAddress { get; set; }
public DateTime LastTimeReceived { get; set; }
public string BloodTypeReceived { get; set; }
}
我的ReceiverController代码如下所示。
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(ReceiverCreationModel model)
{
if (ModelState.IsValid)
{
await receiverService.CreateReceiver(model);
}
return RedirectToAction("Index");
}
接收器服务代码如下所示。
public List<Receiver> GetReceivers()
{
return DbContext.Receivers.ToList();
}
public async Task CreateReceiver(ReceiverCreationModel model)
{
DbContext.Receivers.Add(Mapper.Map<ReceiverCreationModel, Receiver>(model));
await DbContext.SaveChangesAsync();
}
创建视图代码如下所示。
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DonorCreationModels</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ReceiverName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReceiverName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReceiverName, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.ReceiverPhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReceiverPhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReceiverPhoneNumber, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.ReceiverAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ReceiverAddress, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReceiverAddress, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.LastTimeReceived, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastTimeReceived, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastTimeReceived, "", new { @class = "datepicker" })
</div>
@Html.LabelFor(model => model.BloodTypeReceived, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BloodTypeReceived, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BloodTypeReceived, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
答案 0 :(得分:1)
仅当您使用SaveChangesAsync
时才会出现此问题吗?还是SaveChanges
也会发生?