2013-08-30 118 views
0

我有一個問題,劍道下拉列表。向下倒退返回對象對象,但我需要字符串。有人可以幫助我嗎?這裏代碼。我爲我的英語很抱歉,我從俄羅斯很kendo下拉返回對象對象

MyScript.js:(得到管理員控制器數據)

$(document).ready(function() { 
    $("#grid").kendoGrid({ 

     dataSource: { 
      type: 'odata', 
      serverSorting: true, 
      serverFiltering: true, 
      serverPaging: true, 

      transport: { 
       read: { 
        url: "/api/Admin", 
        dataType: "json", 
        contentType: "application/json", 
       }, 
       create: { 
        url: "/api/Admin", 
        dataType: "json", 
        type: "POST" 
       }, 
       update: { 
        url: function (AdminModel) { 
         return "/api/Admin/" + "?roles=" + AdminModel.Roles 
        }, 
        dataType: "json", 
        type: "PUT" 
       }, 
       destroy: { 
        url: function (AdminModel) { 
         return "/api/Admin/" + "?name=" + AdminModel.Name 
        }, 
        dataType: "json", 
        type: "DELETE" 
       }, 
       parameterMap: function (model, operation) { 
        if (operation !== "read" && model) { 
         return kendo.stringify(model); 
        } 
       } 
      }, 
      schema: { 
       data: function (response) { 
        if (response.value !== undefined) 
         return response.value; 
        else { 
         delete response["odata.metadata"]; 
         return response; 
        } 
       }, 
       total: function (response) { 
        return response['odata.count']; 
       }, 
       model: { 
        id: "ID", 
        fields: { 
         ID: { editable: false }, 
         Name: { type: "string", editable: false, nullable: false, validation: { required: true } }, 
         Roles: { type: "string", editable: false }, 
         NewRole: { type: "string" }, 

        } 
       } 
      } 
     }, 


     height: 560, 
     sortable: true, 
     pageable: true, 
     editable: "popup", 
     columns: [ 
      { field: "ID", width: 50 }, 
      { field: "Name", title: "Name", width: 120 }, 
      { field: "Roles", title: "Roles", width: 120}, 
      { field: "NewRole", hidden:true,title: "NewRole", editor: RoleDropDownEditor, template: "#=NewRole#" }, 
      { command: ["destroy", "edit"], title: " ", width: "120px" } 
     ] 
    }); 
}); 
My Drop Down List(get data from Role controller) 
function RoleDropDownEditor(container, options) { 
    $('<input required data-bind="value:' + options.field + '"/>') 
     .appendTo(container) 
     .kendoDropDownList({ 

      dataTextField: "Role", 
      dataValueGield: "Id", 
      autoBind: false, 
      dataSource: { 
       transport: { 
        read: { 

         url: "/api/Role", 
         dataType: "json", 
         contentType: "application/json", 

        }, 
        schema: { 
         model: { 
          fields: { 
           Id:{type:"string"}, 
           Role: { type: "string" } 

          } 
         } 
        } 
       } 


      } 

     }); 
} 

控制器

Web API class Role.cs 

    using MyCMS.Models; 
using System.Web.Security; 

namespace MyCMS.Controllers 
{ 
    public class RoleController : ApiController 
    { 
     private UsersContext db = new UsersContext(); 

     public class MyRole 
     { 
      public string Id { get; set; } 
      public string Role { get; set; } 
     } 

     // GET api/usermanage 

     public List<MyRole> Get() 
     { 
      var result = new List<MyRole>(); 
      foreach (var role in Roles.GetAllRoles()) 
      { 
       result.Add(new MyRole{ Id = role, Role = role}); 
      } 
      return result; 
     } 
    } 
} 

查看

<head> 
    <title></title> 
</head> 

<div id="grid"></div> 

@section Scripts{ 

    @Styles.Render("~/Content/kendo/2013.2.716/css") 
    @Scripts.Render("~/bundles/kendo") 
    @Scripts.Render("~/bundles/Admin") 

} 
+0

'dataValueGield:「Id」'拼寫錯誤... – Brett

+0

謝謝。沒有注意到錯誤。但他仍然返回對象對象 – user2731423

+0

'foreach(var role in Roles.GetAllRoles()){...}' - 在這個語句中什麼數據類型是'role'? – Brett

回答

0

Your dropdownli st的數據源配置不正確。您已經在傳輸中定義了模式。這不是它需要的地方。你把一個大括號放在錯誤的地方。下面是它應該如何看:

dataSource: { 
    transport: { 
    read: { 
     url: "/api/Role", 
     dataType: "json", 
     contentType: "application/json" 
    } 
    }, // YOU WERE MISSING THIS 
    schema: { 
    model: { 
     fields: { 
     Id: { type: "string" }, 
     Role: { type: "string" } 
     } 
    } 
    } 
    // REMOVED AN INCORRECTLY PLACED } FROM HERE 
} 
8

看看這個postKendo Doc。將ValuePrimitive設置爲'true'爲我解決了這個問題。

顯然,如果模型中的字段爲空,Kendo會嘗試將整個項目分配給該字段。將ValuePrimitive設置爲true可以避免這種情況,它只是分配值字段。

+0

最後! - 花了我三天的時間找到這個答案 – Richard