简体   繁体   English

如何向 C# 中的 ASP.NET Core MVC controller 发送数据?

[英]How to send data to the ASP.NET Core MVC controller in C#?

I am trying to send data from my Javascript to my post method controller.我正在尝试将数据从我的 Javascript 发送到我的发布方法 controller。 I have knockout as my method to bind from Javascript to my view.我将淘汰赛作为我从 Javascript 绑定到我的视图的方法。

This is my controller, and this is wher convocationData is null这是我的 controller,这就是 convocationData 是 null

public async Task<ActionResult> CreateConvocation(string convocationData)
{
    string baseUrl = "http://localhost:8080";
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(baseUrl);

This is how I try to call the method using jQuery:这就是我尝试使用 jQuery 调用该方法的方式:

var tmpSession = self.addSession();
var tmpDescription = self.addDesc();
var convocationData = {
            sessionCode: tmpSession,
            description: tmpDescription
        };

$.ajax({
            url: '/Convocations/CreateConvocation',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(convocationData),
            contentType: 'application/json',
            success: function (data) {
                self.Convocations.push(new Convocation(self.addSession(), self.addDesc()));
                self.addDesc("");
                self.addSession("");
            },
            error: function (err) {
                alert(err.status + " : " + err.statusText);
            }
        });

Add [FromBody] attribute like this像这样添加[FromBody]属性

[HttpPost]
public async Task<ActionResult> CreateConvocation([FromBody]string convocationData)
{
    return text;
}

You can read more here Accepting Raw Request Body Content with ASP.NET Web API您可以在此处阅读更多内容 使用 ASP.NET Web API 接受原始请求正文内容

Update:更新:

Also, read zgood's comments to your post.另外,请阅读 zgood 对您的帖子的评论。 Instead of using JSON.stringify you should create a corresponding model as he is describing in his comment.而不是使用JSON.stringify您应该创建一个相应的 model 正如他在评论中所描述的那样。

Update 2:更新 2:

JS model class JS model class

class Convocation {
  constructor(sessionCode, description) {
    this.sessionCode = sessionCode;
    this.description = description;
  }
}

C# DTO class C# DTO class

public class Convocation
{
    public string SessionCode{ get; set; }
    public string Description { get; set; }
}

Then you can update your action method to this然后您可以将您的操作方法更新为此

[HttpPost]
public async Task<ActionResult> CreateConvocation([FromBody]Convocation convocation)
{
    return text;
}

and ajax post to this:和 ajax 发布到此:

$.ajax({
    url: '/Convocations/CreateConvocation',
    type: 'POST',
    dataType: 'json',
    data: new Convocation(tmpSession, tmpDescription),
    contentType: 'application/json',
    success: function(data) {
        self.Convocations.push(new Convocation(self.addSession(), self.addDesc()));
        self.addDesc("");
        self.addSession("");
    },
    error: function(err) {
        alert(err.status + " : " + err.statusText);
    }
});

I didn't test the code but it should work.我没有测试代码,但它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC Send JSON data to highcharts pie from asp.net MVC controller in C# - Send JSON data to highcharts pie from asp.net MVC controller in C# 首先从控制器获取数据,然后如何将其发回? asp.net 核心 mvc - First getting data from controller, then how to send it back? asp.net core mvc ASP.NET MVC C#:将视图数据传递给 controller - ASP.NET MVC C# : passing view data to controller Asp.net 核心 mvc 将 int 数组发送到 controller - Asp.net core mvc send array of int to controller C# &amp; ASP.NET Core MVC: web scraper ajax sends null to controller - C# & ASP.NET Core MVC : web scraper ajax sends null to controller 将模型对象从视图发送到控制器方法ASP.NET MVC C# - send model object from view to controller method ASP.NET MVC C# 使用ASP.NET Core MVC C#将JSON发送到另一台服务器 - Send json to another server using asp.net core mvc c# ASP.NET Core MVC 如何将 ViewModel 发送到不同的控制器方法 - ASP.NET Core MVC How to send ViewModel to different controller method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM