简体   繁体   中英

Pass data from React component to MVC controller

I am trying to pass data that I have gathered as input in a React component to a C# controller in an MVC model. I have had no luck searching for this, and have tried using post , axios and react-router methods to no avail. Unless I am doing it wrong, all these methods seem stuck in the Views and cannot find the controller

I am trying to get the data back to a C# controller to load a new page with information dependent on the input. I posted pseudo code of how my React component looks below.

React Webpage

function GetInput()
{
  var inputVals = ["a", "b", "c"];
  function SendData(valuesToSend)
  {
    //How do I send data?
    loadPageBasedOnInput(valuesToSend);
  }
  return (
    <Button onclick={sendData}>Submit</Button>
  );
}

C# Controller

public static ActionResult loadPageBasedOnInput(string[] inputs)
{
  data = doStuff(inputs);
  return data;

}

I figured out the problem. Because the page is being called from within the current controller, we can interact with it as if it were our current directory. Thus, we can call any other functions within our current controller simply by doing a post as such:

$.post("./function_in_current_controller", data_to_pass_to_function)

I also learned that if you want to call a function located in a different controller, you can call it as such:

$.post("../different_controller/function", data_to_send)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM