我是MVC的新手,需要帮助上传文件,我正在使用umbraco。7.2.1我正在尝试用带有附件的邮件发送邮件以下是我的相同代码。部分视图===>名称联系
using (Html.BeginUmbracoForm<ContactVController>("HandleContactSubmit"))
{
@Html.LabelFor(model => model.Name)<br />
@Html.EditorFor(model => model.Name)<br />
@Html.ValidationMessageFor(model => model.Name)<br />
@Html.LabelFor(model => model.Email)<br />
@Html.EditorFor(model => model.Email)<br />
@Html.ValidationMessageFor(model => model.Email)<br />
<input type="file" name="file" id="file" />
<p>
<input type="submit" value="Submit" />
</p>
}
型号
public class ContactVModel
{
[Required]
public string Name { get; set; }
[Required]
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]
public string Email { get; set; }
[Required]
public string Message { get; set; }
public HttpPostedFileBase attachment { get; set; }
}
控制器
public class ContactVController : SurfaceController
{
[HttpPost]
public ActionResult HandleContactSubmit(ContactVModel model)
{
,.......... ...... ....
,.......... ...... ....
MailBody + = model.Name ;
MailBody + = model.Email;
SendMail( MailBody )
}
但我不知道访问模式。附件,我如何发送带有附件的邮件(上传的文件)(因为我可以访问姓名、电子邮件等)我已经参考了以下帖子,但我无法访问附件
MVC 4 Razor文件上传
但我无法识别
更改的输入
<input type="file" name="file" id="file" />
至
<input type="file" name="attachment" id="attachment" />
因此,模型中的属性与输入字段匹配。
我目前正在使用以下内容:
In the cshtml file:
@Html.UploadFor(model=> model.Attachment)
static public class HtmlExtensions
{
public static IHtmlString UploadFor<TSource, TResult>(this HtmlHelper<TSource> html, Expression<Func<TSource, TResult>> propertyExpression)
{
var memberName = Reflection.GetPropertyName(propertyExpression);
return new HtmlString($"<input type="file" name="{memberName}" id="{memberName}">");
}
}
class Reflection
{
public static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}