如何将节点作为参数与其他类型一起传递给使用ajax在umbraco中的函数
本文关键字:ajax 函数 umbraco 一起 节点 参数 类型 其他 | 更新日期: 2023-09-27 17:49:39
我试图传递一个节点和两个字符串作为参数到一个JsonResult在umbraco。当我调试该方法时,节点var没有值。这里是javascript
function newsSub() {
if(validateForm('newsSubscriptionForm') == true)
{
var name = document.forms["newsSubForm"]["name"].value;//The input name
var email = document.forms["newsSubForm"]["email"].value;//The input email
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';
}
//success:function (result) {//Om de waarde van een string op te halen die naar een jsonresult geretourneerd wordt.
// alert(result);
}
})
document.getElementById("newsSubscriptionForm").style.display='none';
}
};
这里是c#
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());
}
}
//return Json(userData["Name"], JsonRequestBehavior.AllowGet);//Om te kijken of alles dat opgestuurd wordt wel waarde heeft.
if (Mailing.GenerateAndSendMail(userData, iNode))
{
return Json(new { Success = true });
}
}
return Json(new { Success = false });
}
我可能需要转换必须在data后面键入的内容:但如何?
那不可能。我建议你只发送节点id,然后在你的c#中找到节点。
在javascript中找到id:
var nodeId = @CurrentPage.Id;
...
data: {"name":name, "email":email, "nodeId":nodeId},
c#: public JsonResult Newsletter(String name, String email, int nodeId)
{
var node = new Node(nodeId);
....