Home > front end >  cannot pass multiple data parameters from jquery ajax to razor OnPost
cannot pass multiple data parameters from jquery ajax to razor OnPost

Time:05-29

i tried to pass multiple parameters using ajax in so many ways using "data:" but not all parameters are passed.

this is the OnPost PageModel method:

public JsonResult OnPostCreate(Product product, int id, string name)
    {
        return new JsonResult(new { success = true });
    }

ajax:

function SaveMe() {
var product = {
    ProductId: 1,
    ProductName: 'Any',
    UnitsInStock: 2,
    Discontinued: true,
};

var id = 1;
var name = 'Product1';

var data = {};
data.product = product;
data.id = id;
data.name = name;

$.ajax({
    url: `?handler=Create`,
    method: "post",
    contentType: "application/json",
    headers: {
        "XSRF-TOKEN": $('input:hidden[name="__RequestVerificationToken"]').val()
    },
    data: JSON.stringify(data)
})
    .done(function (response) {
        alert(response.success);
    });

};

and i don't want use the parameters in the "url:" ajax like:

url: `?handler=Create&id=2&name=Product1`

can anyone help? thanks for all.

CodePudding user response:

May be this can help you;

function SaveMe() {
  var product = {
      ProductId: 1,
      ProductName: 'Any',
      UnitsInStock: 2,
      Discontinued: true,
  };

  var id = 1;
  var name = 'Product1';
  
  product.id = id; // ----------------------- (*1)
  product.name = name; // ------------------- (*2)

$.ajax({
    url: `?handler=Create`,
    method: "post",
    contentType: "application/json",
    headers: {
        "XSRF-TOKEN": $('input:hidden[name="__RequestVerificationToken"]').val()
    },
    data: product  // ----------------------- (*3)
})
    .done(function (response) {
        alert(response.success);
    });
};

*1 : At the server side, your product model does not have "id" and "name" property but when you want to use them together, you prepare client-side model together.

*2 : Also another option; adding "id" and "name" property to Product model with [NotMapped] attribute. Not mapped attribute can help you to add extra property into model which is not presented at the database table.

 public class Product
 {
     public int ProductId { get; set; }
     public string ProductName { get; set; }
     public int UnitsInStock { get; set; }
     public bool Discontinued { get; set; }
     
     [NotMapped]
     public int id { get; set; }
     
     [NotMapped]
     public string name { get; set; }

 {

*3 : Using JSON.stringify is not neccessery. Post data as a json.

CodePudding user response:

yes true my friend. but what i want is to separate the variables parameters from model, because some parameters type will be bool, string, int ...etc, so i want to avoid the viewmodels as much as i can.

i just tried to use that in MVC with Controllers, and it works fine.

but in Razor Pages, ajax data is not passing whatever i want from complex multiple parameters in "data:" without "url:" handler.

regards,

  • Related