1
我有簡單的數據庫ASP.NET MVC中刪除項目
CREATE TABLE [dbo].[Date] (
[Id] INT NOT NULL,
[Name] NCHAR (10) NULL,
[Date] DATE NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
它包含兩個記錄
我的應用程序加載Name
使用Date
並在屏幕上顯示它的數據庫。
的主要問題是,如果我應該使用Remove
列表中刪除一個項目,比Save data
按鈕單擊它應該傳遞給SaveData
什麼。 我認爲我有一個嚴重的架構錯誤,因爲SaveData
函數不知道需要刪除的記錄。如何解決它?
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<input type="text" value="02.01.2016">
<button data-bind="click: loadData">Load data</button>
<script src="~/Scripts/knockout-2.2.0.js"></script>
<table>
<thead>
<tr>
<th>name</th>
</tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td><input data-bind="value: name" /></td>
<td><a href="#" data-bind="click: $root.removeItems">Remove item</a></td>
</tr>
</tbody>
</table>
<button data-bind="click: saveData">Save data</button>
<script>
function MyViewModel(){
var self = this;
self.items = ko.observableArray();
self.removeItems = function (item) {
self.items.remove(item);
}
self.loadData = function() {
$.ajax({
cache: false,
type: "GET",
url: "Home/GetData",
data: { "date": $("input").val() },
success: function (data) {
$.each(data, function (id, item) {
self.items.push({ name: item.Name });
});
},
error: function (response) {
alert('eror');
}
});
}
self.saveData = function() {
var jsonOfLog = JSON.stringify(self.items());
$.ajax({
type: 'POST',
dataType: 'text',
url: "Home/SaveData",
data: "jsonOfLog=" + jsonOfLog,
success: function (returnPayload) {
console && console.log("request succeeded");
},
error: function (xhr, ajaxOptions, thrownError) {
console && console.log("request failed");
},
processData: false,
async: false
});
}
}
ko.applyBindings(new MyViewModel());
</script>
的HomeController:
public ActionResult Index()
{
return View();
}
modelEntities ME = new modelEntities();
[HttpGet]
public JsonResult GetData(string date)
{
DateTime inputdate = DateTime.Parse(date);
List<Date> list = ME.Date.Where(key => key.Date1 == inputdate).ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public string SaveData(string jsonOfLog)
{
return Convert.ToString(jsonOfLog);
}
'應該傳遞到保存數據nothing'您可以在此更清晰。 –
如果SaveData應該更新記錄,則可能需要使用'Id' – Neps