将项目移除到ASP MVC.NET

时间:2016-05-04 21:48:48

标签: c# asp.net-mvc

我按照在线教程使用ajax动态地将项目添加到绑定列表模型,这非常有效。 (http://www.mattlunn.me.uk/blog/2014/08/how-to-dynamically-via-ajax-add-new-items-to-a-bound-list-model-in-asp-mvc-net/comment-page-2/#comment-68909

我的问题是,如何正确删除列表中的项目? 现在我所做的就是添加一个删除链接,点击该链接会从视图中删除该项目。但是,当我提交表单时,我注意到modelState不再有效,并且它具有从视图中删除的项的空条目。所以我猜这个模型没有更新。

Test.cshtml

@model TesterManager.Models.Test

<div class="form-group">
    @Html.LabelFor(model => model.Software, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <div class="form-group">
            <div class="col-md-5">
                @Html.DropDownListFor(m => m.Software, TesterManager.Models.Helper.GetTestSoftwares(), "Choose a USB Card", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Software, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-5">
                @Html.EditorFor(model => model.Version, new { htmlAttributes = new { @class = "form-control", @title = "Enter a USB FW Version", @placeholder = "Enter a USB FW Version" } })
                @Html.ValidationMessageFor(model => model.Version, "", new { @class = "text-danger" })
            </div>
            <div class="col-md-2">
                <a href="#" id="remove-test" onclick="$(this).parent().parent().parent().parent().remove();">Delete</a>
            </div>
        </div>
    </div>
</div>

AdminTesterConfigurations.cshtml(摘录):

@model TesterManager.Models.AdminTesterConfigurations

<div class="form-group">
    <div class="col-md-6">
        ....
    </div>
</div>
<hr />
<div class="form-group">
    <div class="col-md-12">
        <h3>Test Software</h3>
        <div id="test-list">
            @Html.EditorForMany(x => x.Tests, x => x.Index)
        </div>
        <input type="button" id="add-test" value="Add" />
    </div>
</div>

RequestEditViewModel.cshtml:

@model TesterManager.Models.RequestEditViewModel

<div class="form-horizontal">

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })    
    @Html.EditorFor(model => model.ShippingLocation)
    @Html.EditorFor(model => model.RequesterTesterConfigurations)


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

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>

Edit.cshtml:

@model TesterManager.Models.RequestEditViewModel

@Styles.Render("~/Content/Edit")

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section Scripts
{
    <script>
        jQuery(document).ready(function ($) {
            $('#add-test').on('click', function ()  {
                jQuery.get('/TesterManager/Request/AddTest').done(function (html) {
                    $('#test-list').append(html);
                });
            });
        });
    </script>
}


@using (Html.BeginForm())
{
    <h2>Edit</h2>

    @Html.AntiForgeryToken()
    @Html.EditorFor(x => x);
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

RequestController.cs

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(RequestEditViewModel request)
    {
        if (ModelState.IsValid)
        {
            Request domainRequest = new Request(request);
            requests.Add(domainRequest);
            return RedirectToAction("Index");
        }
        return View(request);
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult AddTest()
    {
        var request = new RequestEditViewModel();
        request.AdminTesterConfigurations.Tests.Add(new Test());

        return View(request);
    }

1 个答案:

答案 0 :(得分:1)

我意识到问题在于,当我从视图中的列表中删除项目时,我也没有删除集合索引器的隐藏输入。我更新了代码以删除隐藏的输入,它现在工作正常。