C# 如何使用Kendo MVVM和c从服务器端API调用发出Kendo数据源请求?
我使用HTML页面作为前端,我必须将一行json数据传输到数据库,我应该如何将一行值作为参数传输到服务器端函数或从服务器端请求数据源来填充实体 以下是我的html代码:C# 如何使用Kendo MVVM和c从服务器端API调用发出Kendo数据源请求?,c#,asp.net,mvvm,kendo-grid,kendo-asp.net-mvc,C#,Asp.net,Mvvm,Kendo Grid,Kendo Asp.net Mvc,我使用HTML页面作为前端,我必须将一行json数据传输到数据库,我应该如何将一行值作为参数传输到服务器端函数或从服务器端请求数据源来填充实体 以下是我的html代码: <div id="example"> <div id="kendoGrid" data-role="grid" data-pageable=" true" data-sortable=" true" data-filterab
<div id="example">
<div id="kendoGrid"
data-role="grid"
data-pageable=" true"
data-sortable=" true"
data-filterable="true"
data-toolbar="['create','save', 'cancel']"
data-editable="inline"
data-columns="[
{ 'field': 'Id', 'width': 100 },
{ 'field': 'ShortName', 'width': 100 },
{ 'field': 'FullName', 'width': 100 },
{ 'field': 'ContactPerson', 'width': 100 },
{ 'field': 'CurrentCurrencyCode', 'width': 100 },
{ 'field': 'Adress1', 'width': 100 },
{ 'field': 'CompanyCity', 'width': 100 },
{ 'field': 'CompanyState', 'width': 100 },
{ 'field': 'CompanyCountry', 'width': 100 },
{ 'field': 'ZipPostCode', 'width': 100 },
{ 'field': 'TelArea', 'width': 100 },
{ command: ['edit', 'update'], title: 'Actions', width: '250px' },
]"
data-bind="source: products"
style=" height :500px"></div>
</div>
这是我正在使用的服务器端代码,当我发出调用请求或如何发出数据源请求以填充实体时,必须用值填充它的实体
[HttpPost]
//product must have the values on view model operation create call and sending product data but it is null
public string SaveDefCompny( DefCompanyDTO product)
{
//return defcompny.save();
RPDBEntities data = new RPDBEntities();
var def = new DefCompany();
{
def.Id = product.Id;
def.CurrentCurrencyCode = product.CurrentCurrencyCode;
def.ShortName = product.ShortName;
def.FullName = product.FullName;
def.ContactPerson = product.ContactPerson;
def.Address1 = product.Address1;
def.CompanyCity = product.CompanyCity;
def.CompanyState = product.CompanyState;
def.CompanyCountry = product.CompanyCountry;
def.ZipPostCode = product.ZipPostCode;
def.TelArea = product.TelArea;
}
data.DefCompanies.Add(def);
data.SaveChanges();
return def.ShortName;
//return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}
参数产品有空值我已经使用参数字符串产品测试过了,但api没有调用我尝试传递字符串值,但api仍然无法调用?我将使用JQuery或AJAX调用将其作为JSON字符串从HTML传递到服务器
然后,在我的控制器中,我将使用post方法:
[HttpPost]
public string SaveDefCompny(string serializeddata)
{
DefCompanyDTO product = NewtonSoft.JsonConvert.DeSerializeObject(serializeddata);
}
我对Telerik在演示中指定的场景使用AJAX绑定:
public ActionResult Products_Create([DataSourceRequest]DataSourceRequest request, ProductViewModel product)
{
if (ModelState.IsValid)
{
using (var northwind = new NorthwindEntities())
{
// Create a new Product entity and set its properties from the posted ProductViewModel
var entity = new Product
{
ProductName = product.ProductName,
UnitsInStock = product.UnitsInStock
};
// Add the entity
northwind.Products.Add(entity);
// Insert the entity in the database
northwind.SaveChanges();
// Get the ProductID generated by the database
product.ProductID = entity.ProductID;
}
}
// Return the inserted product. The grid needs the generated ProductID. Also return any validation errors.
return Json(new[] { product }.ToDataSourceResult(request, ModelState));
}
你可以在Telerik中学到更多
由于您在评论中提到您的数据来自WebAPI,这也可能对您有所帮助
更新:
如果您使用的是剑道网格的MVC包装器,则不会发生这种情况。由于这种ASP.NET MVC行为,网格被配置为发出POST请求。但请确保您已经包含了kendo.aspnetmvc.min.js。更多信息可在中找到
默认情况下,ASP.NET MVC的剑道网格应在
配置为ajax绑定。这是由一个定制的
数据源传输和模式。这些定义见
kendo.aspnetmvc.min.js。请确保此文件包含在
其他剑道JavaScript文件。更多信息可在
介绍帮助主题
解决方案:JavaScript文件的正确顺序
<script src="/Scripts/kendo.web.min.js"></script> <-- or kendo.all.min.js -->
<script src="/Scripts/kendo.aspnetmvc.min.js"></script>
另请参阅 为什么不将其作为JSON从html传递到服务器?当我将JSON数据发送到字符串序列化数据作为参数服务器端时,不允许使用405方法为什么省略[DataSourceRequest]DataSourceRequest请求参数?使用哪个库会产生错误,我必须在方括号中输入datasource名称,我在回复[DataSourceRequest]时发布了我的完整代码DataSourceRequest请求非常重要。[DataSourceRequest]代表负责反序列化的DataSourceRequestStatAttribute。用Kendo.UI.DataSourceRequestAttribute修饰该参数。该属性负责填充DataSourceRequest对象。来源:请查看我的答案,并给出无法将对象隐式转换为DefCompanyDTO显式转换存在的答案?不使用ajax或jquer我必须使用传输方法和api调用非web服务我的数据未提交字符串序列化数据参数,是否只有您的创建无效?您的读取、销毁和更新工作正常?问题解决伙伴在我的设置中我只允许获取和放置
<script src="/Scripts/kendo.web.min.js"></script> <-- or kendo.all.min.js -->
<script src="/Scripts/kendo.aspnetmvc.min.js"></script>