MVC Core发布动态集合

时间:2017-09-14 16:35:36

标签: asp.net ajax asp.net-mvc asp.net-core-mvc

我是MVC的新手并且觉得我在概念上遗漏了一些东西。我试图创建一个页面,我可以一次提交多个记录,通过ajax动态添加和删除记录

我一直在尝试关注Steven Sandersonhaacked等示例,但每当我提交页面时,该集合都包含0条记录。

看着Fiddler我可以看到两个行都被提交但是有些东西丢失了。

我已经看过对编辑器模板和html帮助器的引用,以使这个更漂亮,但这些已经过去了我的头脑,我发现的大多数示例都是旧版本的MVC。

模型

public class Projection
{
    public int ID { get; set; }
    [Display(Name = "Project")]
    public int ProjectId{ get; set; }
    [Display(Name = "Employee")]
    public int EmployeeId { get; set; }
    public decimal Hours { get; set; }
    [DisplayFormat(DataFormatString = "{0:MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Month { get; set; } 
    [Timestamp]
    public byte[] RowVersion { get; set; }

    public virtual Project Project { get; set; }
    public virtual Employee Employee { get; set; }
}

控制器

        // GET: Projections/CreateMulti
    [HttpGet]
    public IActionResult CreateMulti()
    {
        ViewBag.Index = 0;
        ViewData["EmployeeId"] = new SelectList(_context.Employees.Where(e => e.Active).OrderBy(e => e.FullName), "ID", "FullName");
        PopulateClientsDropDownList();
        PopulateProjectsDropDownList();
        return View(new Projection { Month = DateTime.Today.AddMonths(1).AddDays(1 - DateTime.Today.Day) });
    }

    [HttpGet]
    public IActionResult GetNewRow(int index)
    {
        ViewBag.Index = index;
        ViewData["EmployeeId"] = new SelectList(_context.Employees.Where(e => e.Active).OrderBy(e => e.FullName), "ID", "FullName");
        PopulateClientsDropDownList();
        PopulateProjectsDropDownList();
        return PartialView("CreateMultiPartial", new Projection { Month = DateTime.Today.AddMonths(1).AddDays(1 - DateTime.Today.Day) });
    }

    // POST: Projections/CreateMulti
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult CreateMulti(ICollection<Projection> ProjectionList)
    {
        foreach (Projection item in ProjectionList)
        {
            //do something here
        }
        return View(ProjectionList);
    }

查看

@model Test.Models.Projection

@{int Index = 1;
    string newRowUrl = @Url.Action("GetNewRow", "Projections", new { index = @Index });}

<form asp-action="CreateMulti">
    <table class="table" id="myTable">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Project.Client)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Project)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Employee)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Month)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Hours)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @{Html.RenderPartial("CreateMultiPartial", Model);}
        </tbody>
    </table>
    <div><a href="@newRowUrl" class="btn btn-default" style="float: right" id="addNew">Add Another Row</a></div>
    <div>
        <input type="submit" value="Submit" class="btn btn-default" />
    </div>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script type="text/javascript">
            $("#addNew").click(function () {
                event.preventDefault();
                $.ajax({
                    url: $("#addNew").attr("href"),
                    cache: false,
                    success: function (html) {
                        $('#myTable > tbody:last-child').append(html);
                        indexIterate();
                    }
                });
            });

            var currentIndex = 1;

            function indexIterate() {
                var newHref = $("#addNew").attr("href");
                var newerHref = newHref.replace(/(?:index=)[0-9]+/i, "index=" + ++currentIndex);
                $("#addNew").attr("href", newerHref);
            };
        });
    </script>
}

Fiddler

Projections.Index=0&ClientID=7&Projections%5B0%5D.ProjectId=40&Projections%5B0%5D.EmployeeId=3&Projections%5B0%5D.Month=10%2F2017&Projections%5B0%5D.Hours=1&Projections.Index=1&ClientID=15&Projections%5B1%5D.ProjectId=19&Projections%5B1%5D.EmployeeId=32&Projections%5B1%5D.Month=10%2F2017&Projections%5B1%5D.Hours=1&__RequestVerificationToken=AAA

0 个答案:

没有答案