.NET Core/MVC-视图将空模型发布回控制器 [英] .NET Core/MVC - View Posting Null Model Back to Controller

查看:78
本文介绍了.NET Core/MVC-视图将空模型发布回控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using C#/.Net Core and MongoDB. My edit action takes in a model that contains a list of string values and then needs to edit each value in that list. However, when I call the edit (submit) action, my model is null or returns an empty list.

编辑获取:

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

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

I might be missing something fundamental or easy here but any suggestions are very much appreciated!

推荐答案

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

The foreach is the problem. Use a for loop and the index of the collection to get the values to be properly posted back to the controller

例如

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

这篇关于.NET Core/MVC-视图将空模型发布回控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆