.NET Core / MVC-视图将空模型发布回控制器

时间:2018-09-19 16:40:40

标签: c# .net asp.net-core

我正在使用C#/。Net Core和MongoDB。我的编辑操作采用一个包含字符串值列表的模型,然后需要编辑该列表中的每个值。但是,当我调用编辑(提交)操作时,我的模型为null或返回一个空列表。

编辑获取:

        [HttpGet]
        public IActionResult EditUser(string userId)
        {
           if (userId == null)
            return NotFound();

           var user = _mongoService.RecordLookup(userId);

           if (user == null)
            return NotFound();

           var viewModel = new EditRecordViewModel { Records = user };

           return View(viewModel);
        }

编辑帖子:

    [HttpPost]
    public IActionResult EditUser(EditRecordViewModel model)
    {
        try
        {
            foreach (var rec in model.Records)
            {
                var id = rec.Id;
                var filter = Builders<RecordModel>.Filter.Eq("Id", id);

                var modified = rec.Modified_At;
                modified = DateTime.Now;
                var updater = Builders<RecordModel>.Update.Set("Modified_At", modified);

                var val = rec.Value;
                updater = updater.Set("Value", val);

                _mongoService.EditRecord(filter, updater);
            }              
        }

        catch(Exception ex)
        {
            throw ex;
        }

        return RedirectToAction("Index");
    }

查看:

    @model EditRecordViewModel


    @using (Html.BeginForm("EditUser", "Grid", FormMethod.Post))
    {
       <div class="card">           
         <div class="card-body px-lg-5 pt-0 list-group-flush form-horizontal">    
            <div class="row list-group-item form-control">
                 @foreach (var rec in Model.Records)
                    {
                        @rec.ColumnName @Html.TextBoxFor(m => rec.Value, new { @class = "form-control" })
                    }
            </div>
      </div>
      <div class="row list-group-item form-control">
          <div class="col-md-12">
              <input type="submit" class="btn btn-outline-success btn-rounded" 
               value="Save Edits" />
              @Html.ActionLink("Cancel", "Index", "Grid", null, new { @class="btn btn-outline-black btn-rounded" })
        </div>
    </div>
</div>
}

查看模型:

   using System.Collections.Generic;

   namespace Medhub_API.Data.Models
   {
     public class EditRecordViewModel
     {
        public List<RecordModel> Records { get; set; }
     }
   }

在这里我可能缺少一些基本或简单的东西,但是任何建议都非常感谢!

2 个答案:

答案 0 :(得分:1)

foreach是问题所在。使用for循环和集合的索引来获取要正确发布回控制器的值

例如

@model EditRecordViewModel


@using (Html.BeginForm("EditUser", "Grid", FormMethod.Post)) {
   <div class="card">           
     <div class="card-body px-lg-5 pt-0 list-group-flush form-horizontal">          
        <div class="row list-group-item form-control">
            @for(var i = 0; i < Model.Records.Count; i++) {
                @Model.Records[i].ColumnName @Html.TextBoxFor(m => m.Records[i].Value, new { @class = "form-control" })
            }
        </div>
    </div>
  </div>
  <div class="row list-group-item form-control">
      <div class="col-md-12">
          <input type="submit" class="btn btn-outline-success btn-rounded" 
           value="Save Edits" />
          @Html.ActionLink("Cancel", "Index", "Grid", null, new { @class="btn btn-outline-black btn-rounded" })
      </div>
  </div>
}

答案 1 :(得分:0)

替换

@foreach (var rec in Model.Records)
{
    @rec.ColumnName @Html.TextBoxFor(m => rec.Value, new { @class = "form-control" })
}

使用

@Html.EditorFor(model => model.Records)

在EditorTemplates文件夹内添加一个新文件RecordModel.cshtml并添加以下代码

@model IEnumerable<RecordModel>
@foreach(var rec in Model)
{
   @rec.ColumnName @Html.TextBoxFor(m => rec.Value, new { @class = "form-control" })
}