开发者

Using Json and jQuery to populate DropDown from a list of custom classes in Asp.Net MVC 2

开发者 https://www.devze.com 2023-01-06 06:33 出处:网络
I\'m trying to populate a DropDown from a Json result using jQuery. I have the following class: class MyOption

I'm trying to populate a DropDown from a Json result using jQuery.

I have the following class:

class MyOption
{
    int Id { get; set; }
    string Name { get; set; }
}

I'm not sure how to get a list of these into my Json result. At the moment I have the following, I'm not sure if it's correct but it compiles and runs:

return Json(new
       {
           user = userInfo.DisplayName,
           timestamp = DateTime.Now,
           options = GetValidOptions() // returns List<MyOption>
       });

And now in jQuery I'm trying to get the options with things like:

function myFunc(data) {
    var obj = data.get_response().get_ob开发者_如何学运维ject();
    $.each(obj.options, function(option) {
        alert(option.Id + option.Name); // doesn't work
    });
}

What do I need to do to get this working?


You have an example here

0

精彩评论

暂无评论...
验证码 换一张
取 消