【发布时间】:2015-06-29 07:13:28
【问题描述】:
我有一个剑道网格,我有一个自定义命令按钮,当我按下该按钮时,我想打开一个新的剑道窗口,然后通过 ajax 调用获取有关该产品的所有信息并在其中填充几个不同的表单那个窗口。
这是我在弹出窗口中的 html。
<div id="productinformation-form">
<div class="form-group">
<label for="id">Id</label>
<input data-bind="value: id" type="text" class="form-control" id="id" />
</div>
<div class="form-group">
<label for="unitmeasurement">Unit Measurement</label>
<input data-bind="value: unitMeasurement" type="text" class="form-control" id="unitmeasurement" />
</div>
<div class="form-group">
<label for="minorderqty">Unit Measurement</label>
<input data-bind="value: minOrderQty" type="text" class="form-control" id="minorderqty" />
</div>
<div class="form-group">
<label for="packsize">Pack Size</label>
<input data-bind="value: packSize" type="text" class="form-control" id="packsize" />
</div>
<div class="form-group">
<label for="leadTime">Lead Time</label>
<input data-bind="value: leadTime" type="text" class="form-control" id="leadTime" />
</div>
<div class="form-group">
<label for="generalaccessorycategoryid">General Accessory Categoryid</label>
<input data-bind="value: generalAccessoryCategoryId" type="text" class="form-control" id="generalaccessorycategoryid" />
</div>
<div class="form-group">
<label for="company">Company</label>
<input data-bind="value: Company" type="text" class="form-control" id="company" />
</div>
<div class="form-group">
<label for="weight">Weight</label>
<input data-bind="value: Weight" type="text" class="form-control" id="weight" />
</div>
<div class="form-group">
<label for="producttype">Product Type</label>
<input data-bind="value: ProductType" type="text" class="form-control" id="producttype" />
</div>
</div>
这是我得到上面视图的代码。
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var id = dataItem.id;
var company = dataItem.Company;
console.log(id);
$.ajax({
url: '@Url.Action("EditProductView", "Product")',
type: 'POST',
dataType: 'html',
cache: false,
success: function(data) {
bindProductData(id, company, data);
},
error: function(xhr, error) {
},
});
这里是我获取产品信息的地方,然后尝试使用 mvvm 将其绑定到表单。
function bindProductData(id, company, html) {
bindProductInformation(id, company, html);
}
function bindProductInformation(id, company, html) {
$.ajax({
url: '@Url.Action("GetProductInformation", "Product")',
type: 'POST',
dataType: 'html',
data: { id: id, company: company },
cache: false,
success: function (data) {
$("#edit-product-window").kendoWindow({
modal: true
});
$("#edit-product-window").html(html);
console.log("PRODUCT");
console.log(data);
//var viewModel = kendo.observable({
// id: data.id
//});
var window = $("#edit-product-window").data("kendoWindow");
window.open();
window.center();
kendo.bind($("#productinformation-form"), data);
},
error: function (xhr, error) {
console.log("ERROR");
},
});
在我的 kendo.bind 中,对象数据如下所示:
{"id":"1068M","unitMeasurement":"1","minOrderQty":null,"packSize":null,"leadTime":null,"generalAccessoryCategoryId":null,"Company":"NORMSTAHL","Weight":null,"ProductType":1}
所以我认为它应该能够正确绑定到表单。
编辑: 如果我将代码更改为:
var viewModel = kendo.observable({
id: "asdasd"
});
var window = $("#edit-product-window").data("kendoWindow");
window.open();
window.center();
kendo.bind($("#productinformation-form"), viewModel);
它有效。然后它在表单中输入 asdasd 作为 id。
但如果我像这样使用帖子中的数据:
var viewModel = kendo.observable({
id: data.id
});
var window = $("#edit-product-window").data("kendoWindow");
window.open();
window.center();
kendo.bind($("#productinformation-form"), viewModel);
那么它就不会输入了!
编辑 2: 如果我执行 console.log(data),它会在控制台中显示整个对象。
但如果我执行 console.log(data.id),即使数据显示我的对象包含 id,它也会显示我未定义。
{"id":"1062M","unitMeasurement":"1","minOrderQty":null,"packSize":null,"leadTime":null,"generalAccessoryCategoryId":null,"Company":"NORMSTAHL","Weight":null,"ProductType":1}
【问题讨论】:
-
试试
console.log(viewModel)和console.log(data).. -
这对我有什么帮助?
-
你确定它已经是一个对象了吗?尝试使用
JSON.parse(data); -
成功了!但是如果我从我的控制器返回一个 jsonresult,为什么当我在我的视图中收到它时它不是一个 json 对象?
-
在你的ajax请求
dataType: 'html'这就是你的问题,把它改成'json'