我一直在尝试将邮件功能与2sxc mobius应用程序隔离,以便在我自己的2sxc项目中实现它,但到目前为止,我只是成功传递了字符串,字符串字典。如果我尝试使用默认字符串,则对象会给出几个编译不是非常具体的错误。
这就是我现在所做的工作:
查看:
<div>
<div>
<label for="testfield">Test field</label>
</div>
<div>
<input type="text" id="testfield" value="">
</div>
</div>
<div>
<button id="saveData" type="button" onclick="saveMailData()">Guardar dados</button>
</div>
<script type="text/javascript" src="/desktopmodules/tosic_sexycontent/js/2sxc.api.min.js" data-enableoptimizations="100"></script>
<script>
function saveMailData() {
var newItem = {
"user": "@Dnn.User.Username",
"testfield": $("#testfield").val()
};
$2sxc(@Dnn.Module.ModuleID).webApi.post("Form/ProcessForm", {}, newItem, true)
.success(function() {
alert("Success");
})
.error(function() {
alert("Error");
});
}
</script>
控制器:
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.");
}
}
模板:
@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";
}
}
我真的想避免使用动态来接收数据(这对于初学者来说是一场噩梦),那么你能帮我正确地将数据作为对象(字符串,对象)从JS传递给控制器,从控制器传递到剃刀模板吗? / p>
答案 0 :(得分:0)
如果你试图使用你以某种方式假设系统会正确地将这些转换为数字,日期等等。这通常不可靠并且会引起很多副作用。例如,输入字段中的数字将是浏览器中的字符串,因此它也将作为字符串到达服务器中。
日期会更糟糕:它们会被视为字符串 - 并且没有自动检测会将它们变成日期,因为AJAX调用使用的JSON格式没有日期标准。
因此,如果您尝试使用对象方法(因为它不会自动拥有其他类型),基本上数字和日期都不会带来任何好处。所以我不确定是否还有其他好处。还有其他理由吗?