Я использую Codefirst платформы сущностей. Всякий раз, когда я создаю данные, он показывает пустой список. Хотя ошибки нет.
Я уже удалил строки и снова выполнил миграцию. Я обновил базу данных. это не сохранение данных в локальной базе данных.
Код моего профиля картирования указан ниже.
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>
}