Ich bin eine Erweiterungsmethode zu tun, wo ich möchte meine Variable image
gleich zu Namen meiner picKann nicht Variable zugreifen in der Datenbank speichern
Erweiterungsmethode:
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";
}
}
Wie kann ich den Zugriff tun zu meiner Variable pic
und schließlich Post in meinem Modell
Hinweis: "Bildgröße ist Typ string"
---------------------- bearbeiten ------ ----------------
Ich habe ein Problem versucht, neue Schieberegler zu schaffen, ich will es und speichert Bild in Bildvariablen auf Datenbank erstellen
Modell:
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; }
}
Ansicht erstellen:
@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 Erweiterung Methode übergeben es Ausnahme zu fangen und senden es
Ich benutze Debug-Modus, und Dateivariable ist immer Null, aber ich weiß nicht warum
wo machst du die 'Extension Method' ...? Ich sehe den ersten Parameter in Ihrer Methode nicht mit 'this' qualifiziert. – MethodMan
Schreiben Sie den Code des Modells in den Dateizustand, den Sie überprüft haben, andernfalls deklarieren Sie die Bildvariable über dem Dateizustand. –
Ich mache es, da ist mein Modell und View @AkramKhan, jetzt passiere es auf Ausnahmeverweis und ich weiß nicht warum –