I am using entity framework codefirst. Whenever I create the data, it's showing an empty list. Although there is no error.

I have already deleted the rows and did migrations again. I have updated the database. it's not saving the data in local db.

My Mapping Profile code is below.

public class MappingProfile: Profile
{
    public MappingProfile()
    {
        Mapper.CreateMap<DonorCreationModels, Donor>();
        Mapper.CreateMap<Donor, Donor>();

        Mapper.CreateMap<ReceiverCreationModel, Receiver>();
        Mapper.CreateMap<Receiver, Receiver>();
    }
}

Receiver Model is shown below.

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; }
}

Donor Creation Model code is shown below.

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; }
      }

My ReceiverController code is shown below.

public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create(ReceiverCreationModel model)
    {
        if (ModelState.IsValid)
        {
            await receiverService.CreateReceiver(model);
        }

        return RedirectToAction("Index");
    }

Receiver Service code is shown below.

 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();
    }

Create View code is shown below.

@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>

}

1 Answers

1
Steffx115 On

Does the problem only occur if you use SaveChangesAsync? Or does it also occur with SaveChanges?