I'm做我想要的扩展方法我的变量image
等于我的照片无法访问的变量在数据库中保存
扩展方法的名称:
public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file) // file always return null
{
string pic = null;
if (file != null)
{
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
System.Web.HttpContext.Current.Server.MapPath("~/Content/images/slider"), pic);
file.SaveAs(path);
using (var ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
try
{
var createnewslider = new Slider
{
Alt = slider.Alt,
CreationDate = slider.CreationDate,
Description = slider.Description,
IsVisible = slider.IsVisible,
Order = slider.Order,
Subtitle = slider.Subtitle,
Title = slider.Title,
VideoLink = slider.VideoLink,
Image = pic
};
db.SlidersList.Add(createnewslider);
await db.SaveChangesAsync();
return "Slider Photo " + file + "has been created successfull";
}
catch (Exception ex)
{
return ex.InnerException.Message + "Contact to administrator";
}
}
我该怎么做才能访问我的变量pic
终于发布到我的模型
注:“图片变量的类型为字符串”
----------------------编辑------ ----------------
我试图创造新的滑块上的问题,我想创建并存储图像到数据库
模型图像变量:
public class Slider
{
public int SliderId { get; set; }
public int MainPageId { get; set; }
public MainPage MainPage { get; set; }
public string Title { get; set; }
public string Order { get; set; }
public string Subtitle { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public string Alt { get; set; }
public string VideoLink { get; set; }
public DateTime CreationDate { get; set; }
public bool IsVisible { get; set; }
}
创建视图:
@model xxx.Models.Slider
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Slider</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Order, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Order, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Order, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Subtitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Subtitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Subtitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@using (Html.BeginForm("Create", "Slider", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file">Subir imágen:</label>
<input type="file" name="file" id="file" style="width: 100%;" />
}
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Alt, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Alt, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Alt, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.VideoLink, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VideoLink, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.VideoLink, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreationDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreationDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreationDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsVisible, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsVisible)
@Html.ValidationMessageFor(model => model.IsVisible, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
在扩展方法将它传递给赶上异常并返回它
我使用调试模式,并且文件变量始终为空,但我不知道为什么
你在做'Extension Method' ...?我没有看到你的方法中的第一个参数用'this'限定。 – MethodMan
在你检查的文件条件内写模型代码,否则在文件条件之上声明图片变量。 –
我这样做,有我的模型和视图@AkramKhan,现在它传递给异常参考,我不知道为什么 –