在ASP.NET MVC中使用代码优先实体框架无法正确保存数据

时间:2019-05-11 20:48:29

标签: c# jquery asp.net-mvc entity-framework

保存数据时,将使用随机ID 1015和1016进行保存。

我已经完成了所有迁移并更新了数据库,但仍未以列表形式显示正确的数据。

接收方服务如下:

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

public Receiver ReceiverDetails(int id)
{
    return DbContext.Receivers.SingleOrDefault(d => d.ReceiverId == id);
}

接收器控制器如下:

public ActionResult Index()
{
    return View(receiverService.GetReceivers());
}

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

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

    return RedirectToAction("Index");
}

Create.cshtml视图是这样的:

 @model BBProjectnew.Models.Receivers.ReceiverCreationModel

 @{
     ViewBag.Title = "CreateReceivers";
  }

 <h2>Create</h2>

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

我希望数据按顺序保存。现在从1015年开始保存。

enter image description here

0 个答案:

没有答案