1

我正在尝试将一些序列化数据发送到视图并将其绑定到敲除代码。我正在使用 json.net 库进行序列化,因为我想将枚举的常量传递给视图(而不是底层整数。)我不确定我的控制器返回 Json 数据应该是什么样子。这是示例代码:

我将被序列化的视图模型:

public class FranchiseInfoViewModel
    {

        public string FolderName { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]
        public LobbyTemplateOptions LobbyTemplate { get; set; }

        public List<LobbyTemplateOptions> LobbyTemplates { get; set; }

        public void Initialize()
        {
            FolderName = "Test";

            LobbyTemplate = LobbyTemplateOptions.G;
            LobbyTemplates = new List<LobbyTemplateOptions>
                                 {
                                     LobbyTemplateOptions.G,
                                     LobbyTemplateOptions.H,
                                     LobbyTemplateOptions.I
                                 };

枚举:

[JsonConverter(typeof(StringEnumConverter))]
    public enum LobbyTemplateOptions
    {
        G = 7,
        H = 8,
        I = 9
    }

我的淘汰码:

$(function () {
    omega.FranchiseInfo = (function () {
        var FolderName = ko.observable();
        var LobbyTemplates = ko.observableArray([]);

        $.getJSON("FranchiseData", function (data) {
            FolderName(data.FolderName);

            for (var i = 0; i < data.LobbyTemplate.length; i++) {
               LobbyTemplates.push(data.LobbyTemplate[i]);
            }
        });

        return {
            folderName: FolderName,

            lobbyTemplates: LobbyTemplates
        }
    } ());
    ko.applyBindings(omega.FranchiseInfo);
})

        }

我想知道将序列化的 Json 数据传递给视图的控制器应该是什么样子,因为我没有使用过 json.net,而且我对编程比较陌生:

控制器将 Json 数据传递给视图:

 public JsonResult FranchiseData()
            {
                FranchiseInfoViewModel franchiseInfoViewModel = new FranchiseInfoViewModel();
                franchiseInfoViewModel.MapFranchiseInfoToFranchiseInfoViewModel();
                string json = JsonConvert.SerializeObject(franchiseInfoViewModel);

                // this is how I do it with the default Json serializer
               // return Json(franchiseInfoViewModel, JsonRequestBehavior.AllowGet);
            }

如果有人可以发布我的控制器的工作示例,我将不胜感激。谢谢你!

4

1 回答 1

-1

您需要实现 JsonNetResult。

public class JsonNetResult : ActionResult
{
  public Encoding ContentEncoding { get; set; }
  public string ContentType { get; set; }
  public object Data { get; set; }

  public JsonSerializerSettings SerializerSettings { get; set; }
  public Formatting Formatting { get; set; }

  public JsonNetResult()
  {
    SerializerSettings = new JsonSerializerSettings();
  }

  public override void ExecuteResult(ControllerContext context)
  {
    if (context == null)
      throw new ArgumentNullException("context");

    HttpResponseBase response = context.HttpContext.Response;

    response.ContentType = !string.IsNullOrEmpty(ContentType)
      ? ContentType
      : "application/json";

    if (ContentEncoding != null)
      response.ContentEncoding = ContentEncoding;

    if (Data != null)
    {
      JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting };

      JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
      serializer.Serialize(writer, Data);

      writer.Flush();
    }
  }
}

要使用它,在您的情况下,您需要以这种方式重写控制器方法:

public ActionResult FranchiseData()
{
    FranchiseInfoViewModel franchiseInfoViewModel = new FranchiseInfoViewModel();
    franchiseInfoViewModel.MapFranchiseInfoToFranchiseInfoViewModel();

    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.Formatting = Formatting.Indented;
    jsonNetResult.Data = franchiseInfoViewModel;

    return jsonNetResult;
}

(上面 JsonNetResult 的实现取自这篇博文 http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx

于 2012-04-12T23:38:31.330 回答