如果项目已更改,则将数据发送到控制器

时间:2016-03-08 23:38:15

标签: javascript jquery knockout-2.0

我只需要向控制器发送用户更改的项目。例如,如果第一项Jhon更改为Jhon1,则应该发送,如果它再次更改为“Jhon'它不发送。如何检查这个条件?使用JQuery?对于?

@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/knockout-2.2.0.js"></script>

<h2>Index</h2>

<table>
    <thead>
        <tr>
            <th>Passenger name</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: items">
        <tr>
            <td><input data-bind="value: name" /></td>
        </tr>
    </tbody>
</table>
<button data-bind="click: senddata">send</button>


<script>
function MyViewModel() {
    var self = this;
    self.items = ko.observableArray();
    self.items.push({ name: 'Jhon' });
    self.items.push({ name: 'Smith' });

    self.senddata = function () {
        var jsonOfLog = JSON.stringify(self.items());
        debugger;
        $.ajax({
            type: 'POST',
            url: 'Home/ConvertLogInfoToXml',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ jsonOfLog: jsonOfLog }),
            dataType: 'json'
        });
        debugger;
    }
}

ko.applyBindings(new MyViewModel());

控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public string ConvertLogInfoToXml(string jsonOfLog)
    {
        Console.WriteLine(jsonOfLog);
        return Convert.ToString(jsonOfLog);
    }

}

0 个答案:

没有答案