小编Jac*_*ack的帖子

Asp.Net Core Web API 2.2控制器未返回完整的JSON

我的Asp.Net Core Web API 2.2项目中有一个Web API Controller.

Messageboard 模型:

public class MessageBoard
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public ICollection<Message> Messages { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

Message 模型:

public class Message
    {
        public long Id { get; set; }
        public string Text { get; set; }
        public string User { get; set; }
        public DateTime PostedDate { get; set; }

        public long MessageBoardId { …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core asp.net-core-webapi

6
推荐指数
1
解决办法
3398
查看次数

在 ASP.NET Core 2.2 中将局部视图渲染为 HTML 字符串

我正在尝试使用 AJAX 来调用我的控制器并返回一个带有模型作为字符串的局部视图,以便我可以将它注入到我的 HTML 中。我之前在 MVC5 中使用控制器接口完成了此操作,但我似乎无法找到有关如何为 Asp.Net Core 2.2 中的部分视图执行此操作的任何信息。我找到了如何将视图呈现为字符串的示例,但我无法修改它们以适用于局部视图。

控制器动作:

    public JsonResult GetUpsertPartialView(MessageBoard messageBoard)
    {
        Dictionary<string, string> result = new Dictionary<string, string>();
        string errorState = "0";
        string errorMessage = "";

        try
        {
            result["view"] = ""; // My call to render the Partial View would go here.
        }
        catch (Exception)
        {
            errorState = "1";
            errorMessage = "An error was encountered while constructing the View.";
        }
        result["errorState"] = errorState;
        result["errorMessage"] = errorMessage;
        return Json(result);
    }
Run Code Online (Sandbox Code Playgroud)

AJAX 调用:

   $.ajax({
      type: "GET", …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-core-mvc asp.net-core

5
推荐指数
3
解决办法
5895
查看次数

具有外键关系的 ASP.NET Core Web API 和 EF Core 模型

我正在开发一个用于教育目的的基本 Web API 项目,但我在处理 EF 模型关系时遇到了问题。我有2个模型。MessageMessageBoard

public class Message
    {
        public long Id { get; set; }
        public string Text { get; set; }
        public string User { get; set; }
        public DateTime PostedDate { get; set; }

        public long MessageBoardId { get; set; }
        [ForeignKey("MessageBoardId")]
        public MessageBoard MessageBoard { get; set; }
    }

public class MessageBoard
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Description { …
Run Code Online (Sandbox Code Playgroud)

c# asp.net entity-framework asp.net-web-api2 entity-framework-core

2
推荐指数
1
解决办法
6929
查看次数