I have created a form to upload details about an advertisement and I need to upload an image with it.At the end of all my efforts still I see the image column as NULL in the database.HOw do I fix this issue?please note that I am a beginner.
Code for the view
@using (Html.BeginForm("Create", "Advertisement", FormMethod.Post, new { enctype = "multipart/form-data" })) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Advertisement</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OwnerID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OwnerID)
@Html.ValidationMessageFor(model => model.OwnerID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Category,(SelectList)ViewBag.CategoryList)
@Html.ValidationMessageFor(model => model.Category)
</div>
**<div class="editor-label">
@Html.LabelFor(model => model.Image)
</div>
<div class="editor-field">
<input type="file" id="MyFile" runat="server" />
@Html.ValidationMessageFor(model => model.Image)
</div>**
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Post" />
</p>
</fieldset>
}
And the form looks like below
In my model i am using "byte[]" type for the Image property.At the sql database I am using Varbinary(max) type for image column.
code in database table
CREATE TABLE [dbo].[Advertisements] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[OwnerID] INT NOT NULL,
[Date] DATETIME NOT NULL,
[Category] NVARCHAR (MAX) NOT NULL,
[Image] VARBINARY (MAX) NULL,
[Description] NVARCHAR (200) NOT NULL,
CONSTRAINT [PK_dbo.Advertisements] PRIMARY KEY CLUSTERED ([ID] ASC)
);
Controller action
[HttpPost]
[Authorize]
public ActionResult Create([Bind(Exclude = "ID")]Models.Advertisement advertisement) //submits the result
{
if (ModelState.IsValid)
{
var db = new AdvertisementDataContext();
db.Advertisements.Add(advertisement);
db.SaveChanges(); //saved to the database
return RedirectToAction("Index");
}
return Create(); // if there was an error show the form again
}
Use HttpPostedFileBase to send your file to the controller as a parameter ala: MVC 3 file upload and model binding
then you'll need to convert that parameter to a byte array and save it like you are doing (sorry, on phone will try to edit shortly)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.