1
我想將圖像/文件保存到asp.net mvc3項目的sql數據庫中。我發現了一些例子,他們必須將圖像保存到數據庫中,而沒有其他的東西。當我想用我的Controller的「Create」方法在數據庫中添加一個「Inzending」實例時,我還需要將該文件放入一個字節數組中。我必須同時將所有信息插入到我的數據庫中,因爲沒有字段可能爲空。我希望有人可以幫助我,我只是在學校開始,這是我必須解決的第一個真正的大問題(我覺得很難解決)。我的一些代碼,我已經寫:將圖像/文件上傳並轉換爲mssql數據庫
控制器:
[HttpPost]
public ActionResult Create(INZENDING inzending)
{
string user = User.Identity.Name;
var users = db.aspnet_Users.Single(p => p.UserName == user).UserId;
inzending.Userid = users;
int id = db.INZENDINGs.Count() + 1;
inzending.InzendingNr = id;
if (ModelState.IsValid)
{
db.INZENDINGs.Add(inzending);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ErfgoedNr = new SelectList(db.ERFGOEDFICHEs, "ErfgoedNr", "Naam", inzending.ErfgoedNr);
ViewBag.Aard = new SelectList(db.INPUT_AARD, "Aard", "Aard", inzending.Aard);
ViewBag.Type = new SelectList(db.INPUTTYPEs, "type", "type", inzending.Type);
ViewBag.Status = new SelectList(db.STATUS, "Waarde", "Waarde", inzending.Status);
return View(inzending);
}
觀點:
@model Erfgoed.Models.INZENDING
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>INZENDING</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Titel)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Titel)
@Html.ValidationMessageFor(model => model.Titel)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Auteur)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Auteur)
@Html.ValidationMessageFor(model => model.Auteur)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Datum)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Datum)
@Html.ValidationMessageFor(model => model.Datum)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.bestandsgrootte)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.bestandsgrootte)
@Html.ValidationMessageFor(model => model.bestandsgrootte)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Type, "INPUTTYPE")
</div>
<div class="editor-field">
@Html.DropDownList("Type", String.Empty)
@Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Aard, "INPUT_AARD")
</div>
<div class="editor-field">
@Html.DropDownList("Aard", String.Empty)
@Html.ValidationMessageFor(model => model.Aard)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ErfgoedNr, "ERFGOEDFICHE")
</div>
<div class="editor-field">
@Html.DropDownList("ErfgoedNr", String.Empty)
@Html.ValidationMessageFor(model => model.ErfgoedNr)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.bestand, "Bestand");
</div>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
型號:
namespace Erfgoed.Models
{
using System;
using System.Collections.Generic;
public partial class INZENDING
{
public int InzendingNr { get; set; }
public string Titel { get; set; }
public string Auteur { get; set; }
public System.DateTime Datum { get; set; }
public string bestandsgrootte { get; set; }
public string Type { get; set; }
public string Aard { get; set; }
public byte[] bestand { get; set; }
public System.Guid Userid { get; set; }
public int ErfgoedNr { get; set; }
public string Status { get; set; }
public virtual aspnet_Membership aspnet_Membership { get; set; }
public virtual ERFGOEDFICHE ERFGOEDFICHE { get; set; }
public virtual INPUT_AARD INPUT_AARD { get; set; }
public virtual INPUTTYPE INPUTTYPE { get; set; }
public virtual STATUS STATUS1 { get; set; }
}
}