作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 MVC 的新手,需要帮助上传 File ,我正在使用 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 )
}
最佳答案
更改输入
<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;
}
}
关于asp.net-mvc - 如何在 MVC + Umbraco 7 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29353244/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!