简体   繁体   English

删除项目到 ASP MVC.NET 中的绑定列表模型

[英]remove items to a bound list model in ASP MVC.NET

I followed an online tutorial to dynamically add items to a bound list model using ajax, which works perfectly.我按照在线教程使用 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 ) ( 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 )

My question is, how would I correctly remove items from the list?我的问题是,如何正确地从列表中删除项目? Right now what I have done is add a delete link which when clicked removes the item from the view.现在我所做的是添加一个删除链接,单击该链接时会从视图中删除该项目。 However, when I submit the form I noticed that the modelState is no longer valid and it has null entries for the item that was removed from the view.但是,当我提交表单时,我注意到 modelState 不再有效,并且对于从视图中删除的项目,它具有空条目。 So I guess the model is not being updated.所以我猜模型没有更新。

Test.cshtml测试.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 (snippet): 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: 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:编辑.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请求控制器.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);
    }

I realized that the problem was that when I would remove the item from the list in the view, I was not removing the hidden input for the collection indexer as well.我意识到问题在于,当我从视图中的列表中删除项目时,我并没有删除集合索引器的隐藏输入。 I updated the code to remove the hidden input and it works fine now.我更新了代码以删除隐藏的输入,现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM