I have some JQuery that uses Ajax to send information back to my controller to be processed
I am doing it like this:
//Define my controls
<%=Html.TextBox("PName", Model.PName, new { id = "pName" })%> ...
....
....
//Get the values from my controls
var param1= $("#pName").val();
....
....
//Define the return URL. Is this how to send info back?
var url = '<%= Url.Content("~/Port/SaveRowAjax") %>/?ID=' + id
+ "¶m1=" + param1
+ "¶m2=" + param2
+ "¶m3=" + param3
+ "¶m4=" + param4
+ "¶m5=" + param5;
$.ajax({
url: url,
success: function(html) {
alert("Success!");
},
});
//My c# code, that processes the request
public void SaveRowAjax(string param1 ....)
{
...
}
Is this the best way of doing it with MVC?
It seems a bit messy when i am contructing the URL to post back to the server
Try using SerializeArray for submitting your form items. It'll box all their values into a JSON object.
var link = "/Port/SaveRowAjax";
var formData = $(":input").serializeArray();
$.post(link,formData);
You can try to use such syntax with jQuery
$.post(link, {param1: param1, param2: param2 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With