C# 无法访问要保存在数据库中的变量
我正在做一个扩展方法,我希望我的变量C# 无法访问要保存在数据库中的变量,c#,asp.net,asp.net-mvc,entity-framework,entity-framework-6,C#,Asp.net,Asp.net Mvc,Entity Framework,Entity Framework 6,我正在做一个扩展方法,我希望我的变量image等于我的pic的名称 扩展方法: public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file) // file always return null { string pic = null; if (file != null) { pic = System.
image
等于我的pic的名称
扩展方法:
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";
}
}
创建视图:
@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>
@model xxx.Models.Slider
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
滑块
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
@LabelFor(model=>model.Order,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Order,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Order,“,new{@class=“text danger”})
@LabelFor(model=>model.Subtitle,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Subtitle,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Subtitle,“,new{@class=“text danger”})
@LabelFor(model=>model.Description,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Description,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Description,“,new{@class=“text danger”})
@LabelFor(model=>model.Image,htmlAttributes:new{@class=“controllabel col-md-2”})
@使用(Html.BeginForm(“创建”、“滑块”、FormMethod.Post、,
新的{enctype=“multipart/form data”})
{
苏比尔·伊马根:
}
@LabelFor(model=>model.Alt,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Alt,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Alt,“,new{@class=“text danger”})
@LabelFor(model=>model.VideoLink,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.VideoLink,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.VideoLink,“,new{@class=“text danger”})
@LabelFor(model=>model.CreationDate,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CreationDate,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CreationDate,“,new{@class=“text danger”})
@LabelFor(model=>model.IsVisible,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.IsVisible)
@Html.ValidationMessageFor(model=>model.IsVisible,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
在扩展方法中,它传递捕获异常并返回它
我使用调试模式,文件变量总是空的,但我不知道为什么这是因为
pic
在createnewslider
对象的范围内不可用。只需在if语句之外声明pic
变量:
string pic = string.Empty;
if (file != null) {
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
HostingEnvironment.MapPath("~/Content/images/slider"), pic);
file.SaveAs(path);
using (MemoryStream ms = new MemoryStream()) {
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
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
};
在映射到
图像之前检查内容这是因为pic
在createnewslider
对象的范围内不可用。只需在if语句之外声明pic
变量:
string pic = string.Empty;
if (file != null) {
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
HostingEnvironment.MapPath("~/Content/images/slider"), pic);
file.SaveAs(path);
using (MemoryStream ms = new MemoryStream()) {
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
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
};
并在映射到图像之前检查内容
将其定义在与构造函数相同的范围内,以便构造函数可以访问它
public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file)
{
string pic = null;
if (file != null)
{
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
HostingEnvironment.MapPath("~/Content/images/slider"), pic);
file.SaveAs(path);
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
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 // I can´t access to pic variable
};
db.SlidersList.Add(createnewslider);
await db.SaveChangesAsync();
return "Slider Photo " + file + "has been created";
}
公共异步任务CreateNewSlider(滑块,HttpPostedFileBase文件)
{
字符串pic=null;
如果(文件!=null)
{
pic=System.IO.Path.GetFileName(file.FileName);
字符串路径=System.IO.path.Combine(
HostingEnvironment.MapPath(“~/Content/images/slider”),pic);
file.SaveAs(路径);
使用(MemoryStream ms=new MemoryStream())
{
file.InputStream.CopyTo(ms);
字节[]数组=ms.GetBuffer();
}
}
var createnewslider=新滑块
{
Alt=滑块。Alt,
CreationDate=滑块。CreationDate,
Description=滑块。Description,
IsVisible=滑块。IsVisible,
顺序=滑块。顺序,
副标题=滑块。副标题,
标题=滑块。标题,
VideoLink=slider.VideoLink,
Image=pic//我无法访问pic变量
};
db.SlidersList.Add(createnewslider);
等待db.saveChangesSync();
返回“滑块照片”+文件+“已创建”;
}
在与构造函数相同的范围内定义它,以便构造函数可以访问它
public async Task<string> CreateNewSlider(Slider slider, HttpPostedFileBase file)
{
string pic = null;
if (file != null)
{
pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(
HostingEnvironment.MapPath("~/Content/images/slider"), pic);
file.SaveAs(path);
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
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 // I can´t access to pic variable
};
db.SlidersList.Add(createnewslider);
await db.SaveChangesAsync();
return "Slider Photo " + file + "has been created";
}
公共异步任务CreateNewSlider(滑块,HttpPostedFileBase文件)
{
字符串pic=null;
如果(文件!=null)
{
pic=System.IO.Path.GetFileName(file.FileName);
字符串路径=System.IO.path.Combine(
HostingEnvironment.MapPath(“~/Content/images/slider”),pic);
file.SaveAs(路径);
使用(MemoryStream ms=new MemoryStream())
{
file.InputStream.CopyTo(ms);
字节[]数组=ms.GetBuffer();
}