Javascript 将对象从JS传递到controller和razor邮件模板
我一直在尝试将邮件功能从2sxc mobius应用程序中分离出来,以便在我自己的2sxc项目中实现它,但到目前为止,我只在传递字符串、字符串字典方面取得了成功。如果我尝试使用默认字符串,它会给出一些不太具体的错误 以下是我目前的工作: 视图:Javascript 将对象从JS传递到controller和razor邮件模板,javascript,c#,razor,2sxc,Javascript,C#,Razor,2sxc,我一直在尝试将邮件功能从2sxc mobius应用程序中分离出来,以便在我自己的2sxc项目中实现它,但到目前为止,我只在传递字符串、字符串字典方面取得了成功。如果我尝试使用默认字符串,它会给出一些不太具体的错误 以下是我目前的工作: 视图: 试验场 瓜达尔护墙板 函数saveMailData(){ var newItem={ “用户”:“@Dnn.user.Username”, “testfield”:$(“#testfield”).val() }; $2sxc(@Dnn.Module.Mo
试验场
瓜达尔护墙板
函数saveMailData(){
var newItem={
“用户”:“@Dnn.user.Username”,
“testfield”:$(“#testfield”).val()
};
$2sxc(@Dnn.Module.ModuleID).webApi.post(“Form/ProcessForm”,{},newItem,true)
.success(函数(){
警惕(“成功”);
})
.错误(函数(){
警报(“错误”);
});
}
控制器:
using DotNetNuke.Security;
using DotNetNuke.Web.Api;
using System.Web.Http;
using ToSic.SexyContent.WebApi;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Web.Compilation;
using System.Runtime.CompilerServices;
using DotNetNuke.Services.Mail;
using Newtonsoft.Json;
public class FormController : SxcApiController
{
[HttpPost]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Anonymous)]
[ValidateAntiForgeryToken]
public void ProcessForm([FromBody]Dictionary<string,string> contactFormRequest)
{
string mailFrom = "x@x.pt";
string mailTo = "y@y.com";
string mailCc = "z@z.com";
string mailReply = "w@w.pt";
string mailSubject = "THIS IS THE SUBJECT " + contactFormRequest["user"].ToString();
string mailbody = "<table><tr><td>THIS IS THE MESSAGE BODY</td></tr></table>";
var ownerMailEngine = TemplateInstance("testmailtemplate.cshtml");
var ownerBody = ownerMailEngine.Message(contactFormRequest, this).ToString();
var ownerSubj = ownerMailEngine.Subject(contactFormRequest, this);
Mail.SendMail(mailFrom, mailTo, mailCc, "", mailReply, MailPriority.Normal, ownerSubj, MailFormat.Html, System.Text.Encoding.UTF8, ownerBody, new string[0], "", "", "", "", false);
}
private dynamic TemplateInstance(string fileName)
{
var compiledType = BuildManager.GetCompiledType(System.IO.Path.Combine("~", App.Path, fileName));
object objectValue = null;
if (compiledType != null)
{
objectValue = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(compiledType));
return ((dynamic)objectValue);
}
throw new Exception("Error while creating mail template instance.");
}
}
使用DotNetNuke.Security;
使用DotNetNuke.Web.Api;
使用System.Web.Http;
使用ToSic.SexyContent.WebApi;
使用System.Collections.Generic;
使用制度;
使用System.Linq;
使用System.Web.compilement;
使用System.Runtime.CompilerServices;
使用DotNetNuke.Services.Mail;
使用Newtonsoft.Json;
公共类FormController:SXCapicController
{
[HttpPost]
[DNNModuleAuthorization(AccessLevel=SecurityAccessLevel.Anonymous)]
[ValidateAntiForgeryToken]
public void ProcessForm([FromBody]字典contactFormRequest)
{
字符串mailFrom=”x@x.pt";
字符串mailTo=”y@y.com";
字符串mailCc=”z@z.com";
字符串mailReply=”w@w.pt";
string mailSubject=“这是主题”+contactFormRequest[“用户”]。ToString();
string mailbody=“这是邮件正文”;
var ownerMailEngine=TemplateInstance(“testmailtemplate.cshtml”);
var ownerBody=ownerMaileEngine.Message(contactFormRequest,this.ToString();
var ownerSubj=ownerMaileEngine.Subject(contactFormRequest,this);
Mail.SendMail(mailFrom、mailTo、mailCc、“、mailReply、MailPriority.Normal、ownerSubj、MailFormat.Html、System.Text.Encoding.UTF8、ownerBody、新字符串[0]、“”、“”、“”、“”、false);
}
私有动态模板实例(字符串文件名)
{
var compiledType=BuildManager.GetCompiledType(System.IO.Path.Combine(“~”,App.Path,fileName));
objectValue=null;
if(compiledType!=null)
{
objectValue=RuntimeHelpers.GetObjectValue(Activator.CreateInstance(compiledType));
返回((动态)objectValue);
}
抛出新异常(“创建邮件模板实例时出错”);
}
}
和模板:
@helper Message(Dictionary<string,string> request, ToSic.SexyContent.IAppAndDataHelpers context)
{
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body { font-family: Helvetica, sans-serif; }
</style>
</head>
<body>
<h1>Website contact form request</h1>
<p>Key/Value:</p>
<table width="100%">
@foreach (var item in request)
{
<tr>
<td width="10%"><b>@item.Key.ToString()</b></td>
<td>@item.Value.ToString()</td>
</tr>
}
</table>
</body>
</html>
}
@functions {
public string Subject(dynamic request, dynamic helpers) {
return "this is a subject from template";
}
}
@helper消息(字典请求,ToSic.SexyContent.IAppAndDataHelpers上下文)
{
正文{字体系列:Helvetica,无衬线;}
网站联络表格申请
键/值:
@foreach(请求中的var项)
{
@item.Key.ToString()
@item.Value.ToString()
}
}
@功能{
公共字符串主题(动态请求、动态帮助){
返回“这是模板中的主题”;
}
}
我真的希望避免使用dynamic来接收数据(对于初学者来说,这是一场噩梦),所以您能帮助我正确地将数据作为对象(字符串、对象)从JS传递到controller,从controller传递到razor模板吗?如果您尝试使用dynamic,您会认为系统会正确地将其转换为数字、日期等。这通常是不可靠的,会引起很多副作用。例如,输入字段中的数字在浏览器中是一个字符串,因此在服务器中也是一个字符串 日期会更糟糕:它们会被视为字符串——没有自动检测会将它们转换为日期,因为AJAX调用使用的JSON格式没有日期标准 因此,如果您尝试对象方法,基本上数字和日期都不会给您带来任何好处(因为它不会自动具有其他类型)。所以我不确定是否还有其他好处。还有其他理由这样做吗
@helper Message(Dictionary<string,string> request, ToSic.SexyContent.IAppAndDataHelpers context)
{
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body { font-family: Helvetica, sans-serif; }
</style>
</head>
<body>
<h1>Website contact form request</h1>
<p>Key/Value:</p>
<table width="100%">
@foreach (var item in request)
{
<tr>
<td width="10%"><b>@item.Key.ToString()</b></td>
<td>@item.Value.ToString()</td>
</tr>
}
</table>
</body>
</html>
}
@functions {
public string Subject(dynamic request, dynamic helpers) {
return "this is a subject from template";
}
}