从具有敲除绑定的表中删除记录

时间:2015-01-16 12:11:28

标签: javascript asp.net-mvc-4 knockout.js

我是MVC和淘汰赛的新手。 我在视图中有以下表格,它正在使用knockout绑定。

  @using PagedList;
  @using PagedList.Mvc;
  @model IPagedList<DEMO_JAN14.Models.Provider>
  @{
       Layout = "~/Views/Shared/_Layout.cshtml";
   }
 <head>
   <title>LIST OF PROVIDERS</title>
</head>
 <body>
    <div class="container">
        <h1>Providers List</h1><br/>
    <a class="btn btn-primary col-sm-1" href="/Provider/Create">Create</a>     <br/>

    <table class="table table-bordered" id="tblData">
    <tr>
        <th>Provider Type</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Certification</th>
        <th>Specialization</th>
        <th>SSN</th>
        <th>Facility Name</th>
        <th>Contact No</th>
        <th>Contact Email</th>
        <th>Delete</th>
    </tr>
    <tbody data-bind="foreach:viewModel.DataList">
        <tr>
            <td class="col-lg-2" data-bind="text: ProviderType"></td>
            <td class="col-lg-2" data-bind="text: FirstName"></td>
            <td class="col-lg-2" data-bind="text: LastName"></td>
            <td class="col-lg-2" data-bind="text: Certification"></td>
            <td class="col-lg-2" data-bind="text: Specialization"></td>
            <td class="col-lg-2" data-bind="text: SSN"></td>
            <td class="col-lg-2" data-bind="text: FacilityName"></td>
            <td class="col-lg-2" data-bind="text: ContactNumber"></td>
            <td class="col-lg-2" data-bind="text: ContactEmail"></td>
            <td><button type="button" id="Delete" class="btn btn-primary"       data-bind="click:$root.Delete">Delete</button></td>
        </tr>
     </tbody>
  </table>


      <script type="text/javascript" src="../../Scripts/knockout.mapping-latest.js"></script>
     <script type="text/javascript" src="~/Scripts/Index.js"></script>

    <script type="text/javascript">
    var data = @Html.Raw(Json.Encode(Model));
    //viewModel.DataList(ko.mapping.fromJS(data));
    //ko.applyBindings(viewModel2, $("#tblData")[0]);
    </script>

Index.js代码如下。

 function ProviderViewModel() {
var self = this;
self.ProviderID = ko.observable("");
self.ProviderType = ko.observable("");
self.FirstName = ko.observable("");
self.LastName = ko.observable("");
self.Certification = ko.observable("");
self.Specialization = ko.observable("");
self.SSN = ko.observable("");
self.ContactNumber = ko.observable("");
self.ContactEmail = ko.observable("");
self.FacilityName = ko.observable("");
self.DataList = ko.observableArray([]);

 var Provider =
    {
        ProviderID: self.ProviderID,
        ProviderType: self.ProviderType,
        FirstName: self.FirstName,
        LastName: self.LastName,
        Certification: self.Certification,
        Specialization: self.Specialization,
        SSN: self.SSN,
        ContactNumber: self.ContactNumber,
        ContactEmail: self.ContactEmail,
        FacilityName: self.FacilityName,
    };

self.Provider = ko.observable();
self.Providers = ko.observableArray();

// Delete Provider
self.Delete = function (Provider) {
    if (confirm('Are you sure you want to delete this row?')) {
        var id = Provider.ProviderID();

        $.ajax({
            url: "/Provider/Delete",
            cache: false,
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(id),
            success: function (data) {
                self.Providers.remove(Provider);
                //   alert("Record Deleted Successfully");
            }
        }).fail(
             function (xhr, textStatus, err) {
                 alert(err);
             });
       }
    }  //Delete Provider

}

 var viewModel = new ProviderViewModel();

 $(document).ready(function () {
if (data)
    viewModel.DataList(ko.mapping.fromJS(data)());
ko.applyBindings(viewModel);
});

控制器动作方法如图所示。

  [HttpPost]
    public ActionResult Delete(int id)
    {
        DAL.DeleteProvider(id);
        return RedirectToAction("Index");
    }

记录根本没有删除,我收到以下错误。 没有找到网址。请你指导我正确的方向。谢谢。

1 个答案:

答案 0 :(得分:1)

我没有发送要删除的行的ID url应该是url:“/ Provider / Delete /”+ Provider.ProviderID()

现在工作正常。

虽然没有重定向到“索引”操作方法。我必须刷新页面才能看到已删除的记录。

任何指导都会对你有所帮助。谢谢。