When attempting to pass a node and two strings as parameters to a JsonResult in Umbraco, I encountered an issue where the node variable had no value when debugging the method. Below is the JavaScript code:
function newsSub() {
if(validateForm('newsSubscriptionForm') == true)
{
var name = document.forms["newsSubForm"]["name"].value;
var email = document.forms["newsSubForm"]["email"].value;
var node = '@SiteNode';
$.ajax({
type: 'POST',
url: '/Umbraco/Surface/AjaxSurface/Newsletter',
data: {"name":name, "email":email, "node":node},
dataType: 'json',
cache:false,
success:function(data){
var success = data.Success;
var test = data.test;
if(success == true){
document.getElementById("sendTrue").style.display='inline';
}
else{
document.getElementById("sendFalse").style.display='inline';
}
})
document.getElementById("newsSubscriptionForm").style.display='none';
}
};
Here is the corresponding C# code:
public JsonResult Newsletter(String name, String email, Node node)
{
INode iNode = node;
if (ModelState.IsValid)
{
NewsSubscriptiontFormModel model = new NewsSubscriptiontFormModel();
model.Email = email;
model.Name = name;
Dictionary<String, String> userData = new Dictionary<String, String>();
foreach (var prop in typeof(NewsSubscriptiontFormModel).GetProperties())
{
if (prop.GetValue(model, null) != null)
{
userData.Add(prop.Name, (prop.GetValue(model, null) ?? "").ToString());
}
}
if (Mailing.GenerateAndSendMail(userData, iNode))
{
return Json(new { Success = true });
}
}
return Json(new { Success = false });
}
There seems to be a need to convert whatever needs to be typed after the 'data:' in the JavaScript code. How can this be achieved?