skip to Main Content

I’ve a c# class like below sample code

public IActionResult OnPostTranJarray(int transaction_number, string cardholder)
    {
    
    dynamic Trans = GetTransactions(transaction_number,cardholder);
    
    return Trans;
   }

OR

public JArray Transactions { get; set; }
public void OnGet(int transaction_number)
{
  Transactions =  GetTransactions(transaction_number);   
}

just wanted to Make a post call inside the IndexModel Class. I’ve use these below code in jquery

 $.post("https://localhost:7197/Transactions?handler=TranJarray?transaction_number="+transactionNumb+"&cardholder="+cardHolderName, response => { 
      alert("response",response);
    });

$.ajax({
type: "POST",
url: "https://localhost:7197/Transactions?handler=TranJarray?transaction_number="+transactionNumb+"&cardholder="+cardHolderName,
contentType: "application/json; charset=utf-8",
dataType: "json",    
async: true,
cache: false,
success: function (data)
{  
    alert("success");
    fnLoadTbl();
  
}
});

But it’s not hitting to the method.. but if i make ajax GET Request to OnGet method it’s hitting but not returning any value. so let me know if you have any idea. Thanks..

2

Answers


  1. Chosen as BEST ANSWER

    Finally i got the solution by doing these

    1. We've to create a model class like this

      public class PersonModel
       {  [BindProperty]
           public int TransactionNumber { get; set; }
           [BindProperty]
           public string FormName { get; set; }
       }
      

    2.Created a onpost method

     public void  OnPostSubmit(PersonModel person)
        {        
            this.trnTable =   GetTransactions(person.TransactionNumber,
            person.FormName);       
           
        }
    

    3.In frontend we should have and a type="submit" button inside it

     <form id="statementId" method="post">
        <div class="row">
            <dl  class=" col-2 dl-horizontal">
                    <div class="form-group" style="max-width: 200px;">
                     <label>Form</label>
                    <input id="form" name="FormName" type="text" class="form-control" placeholder="Form">
                     <i class="form-group__bar"></i>
                </div>
            </dl>
        
            <dl class=" col-2 dl-horizontal">
            <div class="form-group" style="max-width: 200px;">
                                <label>Transaction Number</label>
                                 <input type="text" name="TransactionNumber" placeholder="Type" class="form-control" >
                                  <i class="form-group__bar"></i>
            </div>
            </dl>
         </div>
                <input style="max-width: 100px;max-height: 40px;margin-left: 10px;"  class="btn btn-primary" type="submit" value="Submit" asp-page-handler="Submit"/>
    </form>
    

    Then it's perfectly working.. but thanks for those who tried to answer it


  2. In order to return JSON from your OnGet method, change the return type from void to IActionResult and return a JsonResult, e.g.:

    public JArray Transactions { get; set; }
    
    public IActionResult OnGet(int transaction_number)
    {
      Transactions =  GetTransactions(transaction_number);   
      return new JsonResult(Transactions);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search