0

我有一个责任类,其中包含一些子类下面是该类:-

public class Responsibility
{
    private Frequency _FrequencyInfo = new Frequency();
    private ResponsibilityCategory _CategoryInfo = new ResponsibilityCategory();
    private Status _StatusInfo = new Status();

    public int ResponsibilityId { get; set; }
    public string ResponsibilityName { get; set; }
    public string Description { get; set; }
    public Frequency FrequencyInfo
    {
        get { return _FrequencyInfo; }
        set { _FrequencyInfo = value; }
    }
    public ResponsibilityCategory CategoryInfo
    {
        get { return _CategoryInfo; }
        set { _CategoryInfo = value; }
    }
    public Status StatusInfo
    {
        get { return _StatusInfo; }
        set { _StatusInfo = value; }
    }
}

下面是我用来绑定列表的代码。但我得到了类的所有元素。我只想要类中的两个元素。

Responsibility newResponsibilty = new Responsibility();
        newResponsibilty.ResponsibilityId = ResponsibilityId;
        new iNGRID_Data.Ops.DataMethods().dbrGetResponsibilityDetailsInSpecifiedResponsibilty(ref newResponsibilty, ResponsibilityId, ref err);
        List<Responsibility> listResponsibilty = new List<Responsibility>();
        listResponsibilty.Add(newResponsibilty);

        JavaScriptSerializer jsonserialize = new JavaScriptSerializer();
        string result = jsonserialize.Serialize(listResponsibilty);
        return Content(result);

我想用 2 个对象 ResponsibiltyName 和 Description 序列化这个列表。请建议。

4

1 回答 1

1

不要手动 JSON 序列化它们。通过返回 Json 结果将其留给框架。你也不需要清单。您可以返回一个包含您感兴趣的属性的匿名对象:

public ActionResult SomeAction()
{
    Responsibility newResponsibilty = new Responsibility();
    newResponsibilty.ResponsibilityId = ResponsibilityId;
    new iNGRID_Data.Ops.DataMethods().dbrGetResponsibilityDetailsInSpecifiedResponsibilty(ref newResponsibilty, ResponsibilityId, ref err);
    var result = new
    {
        name = newResponsibilty.ResponsibiltyName, 
        description = newResponsibilty.Description
    };
    return Json(result, JsonRequestBehavior.AllowGet);
}

在您的 AJAX 成功回调中,您可以读取这些属性:

success: function(result) {
    // you could use result.name and result.description here
}
于 2012-09-18T11:17:52.050 回答