我的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) 我正在尝试使用 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) 我正在开发一个用于教育目的的基本 Web API 项目,但我在处理 EF 模型关系时遇到了问题。我有2个模型。Message和MessageBoard。
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