无法访问变量数据库来保存 [英] Cannot access variable to save in database
问题描述
I'm这样做,我想我的变量图片
扩展方法等于我的照片的名称
I´m doing an extension method where I want to my variable image
equals to name of my 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";
}
}
我
该怎么做才能进入我的变量图片
终于发布到我的模型
请注意:图像变量的类型为字符串
Note: "Image variable is type string"
----------------------编辑----------------------
----------------------Edit----------------------
我有一个问题想创造新的滑块,我想创建并存储图像插入数据库映像变量
I have a problem trying to create new slider, I want to create it and store image into image variable on database
型号:
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>
在扩展方法将它传递给赶上的异常,并将其返回
In extension Method it pass to catch exception and return it
我使用调试模式,以及文件变量总是空,但我不知道为什么。
I use debug mode, and file variable is always null but I don't know why
推荐答案
定义它在同一范围内的构造,这样的构造将访问它。
Define it in the same scope as the constructor so that the constructor will have access to it.
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";
}
这篇关于无法访问变量数据库来保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!