C# 如何在不在模型中的情况下从视图上载或下载文件

C# 如何在不在模型中的情况下从视图上载或下载文件,c#,razor,asp.net-mvc-5,C#,Razor,Asp.net Mvc 5,我正在开发一个依赖于现有SQL Server数据库的应用程序。我使用VisualStudio中的实体框架代码优先方法创建了模型、控制器和创建视图。目前,代码适用于所有字段,并接受应包含外部文件路径(不在数据库中)的字段的null 模型: namespace Centurion.Models { using System; using System.Collections.Generic; using System.Component

我正在开发一个依赖于现有SQL Server数据库的应用程序。我使用VisualStudio中的实体框架代码优先方法创建了模型、控制器和创建视图。目前,代码适用于所有字段,并接受应包含外部文件路径(不在数据库中)的字段的
null

模型:

    namespace Centurion.Models
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Data.Entity.Spatial;

        [Table("Fontane")]
        public partial class Fontane
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Fontane()
            {
                Interventi = new HashSet<Interventi>();
            }

            [Key]
            //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int FontanaID { get; set; }

            private string _CodificaSIMU;

            [StringLength(20)]
            public string CodificaSIMU
            {
                get
                {
                    if (string.IsNullOrEmpty(_CodificaSIMU))
                    {
                        return _CodificaSIMU;
                    }
                    return _CodificaSIMU.ToUpper();
                }
                set
                {
                    _CodificaSIMU = value;
                }
            }
            //public string CodificaSIMU { get; set; }

            [Required]
            [StringLength(255)]
            public string Nome { get; set; }

            [StringLength(255)]
            public string Indirizzo { get; set; }

            public int? Municipio { get; set; }

            [Required]
            public double Latitudine { get; set; }

            [Required]
            public double Longitudine { get; set; }

            [Required]
            public int TipoFontana { get; set; }

            [Required]
            public int TipoImpianto { get; set; }

            [Required]
            public int Frequenza { get; set; }

            [Required]
            public bool Svuotamento { get; set; }

            public bool? Active { get; set; }

            public bool? Allarme { get; set; }

            [StringLength(255)]
            public string Foto { get; set; }

            [StringLength(255)]
            public string Allegato1 { get; set; }

            [StringLength(255)]
            public string Allegato2 { get; set; }

            public string Note { get; set; }

            [Required]
            public int UserIDCrea { get; set; }

            [Required]
            public DateTime DataCrea { get; set; }

            [Required]
            public int UserIDModifica { get; set; }

            [Required]
            public DateTime DataModifica { get; set; }

            public virtual Municipi Municipi { get; set; }

            public virtual TipiFontana TipiFontana { get; set; }

            public virtual Impianto Impianto { get; set; }

            public virtual Utenti Utenti { get; set; }

            public virtual Utenti Utenti1 { get; set; }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Interventi> Interventi { get; set; }

        }

        public class FontaneFiles
        {
            [DataType(DataType.Upload)]
            [Display(Name = "Upload Foto")]
            public string FotoFile { get; set; }

            [DataType(DataType.Upload)]
            [Display(Name = "Upload Allegato1")]
            public string All1File { get; set; }

            [DataType(DataType.Upload)]
            [Display(Name = "Upload Allegato2")]
            public string All2File { get; set; }
        }
    }
观点:

    @model Centurion.Models.Fontane

    @{
        ViewBag.Title = "Crea";
    }

    <h2>Aggiungi Fontana</h2>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CodificaSIMU, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CodificaSIMU, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CodificaSIMU, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
            </div>
        </div>

        <fieldset>
            <legend>Ubicazione</legend>

            <div class="form-group">
                @Html.LabelFor(model => model.Indirizzo, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Indirizzo, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Indirizzo, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Municipio, "Municipio", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("Municipio", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Municipio, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Latitudine, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Latitudine, new
               {
                   htmlAttributes = new
                   {
                       placeholder = "Latitudine = 41,..... (con la virgola)",
                       @class = "form-control"
                   }
               })
                    @Html.ValidationMessageFor(model => model.Latitudine, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Longitudine, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Longitudine, new
               {
                   htmlAttributes = new
                   {
                       placeholder = "Longitudine = 12,..... (con la virgola)",
                       @class = "form-control"
                   }
               })
                    @Html.ValidationMessageFor(model => model.Longitudine, "", new { @class = "text-danger" })
                </div>
            </div>
        </fieldset>

        <div class="form-group">
            @Html.LabelFor(model => model.TipoFontana, "TipoFontana", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("TipoFontana", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TipoFontana, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TipoImpianto, "TipoImpianto", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("TipoImpianto", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TipoImpianto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Frequenza, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Frequenza, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Frequenza, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Svuotamento, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Svuotamento)
                    @Html.ValidationMessageFor(model => model.Svuotamento, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        @Html.HiddenFor(m => m.Active)
        @Html.HiddenFor(m => m.Allarme)

        <div class="form-group">
            @Html.LabelFor(model => model.Foto, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Foto, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Foto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Allegato1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Allegato1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Allegato1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Allegato2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Allegato2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Allegato2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Note, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Note, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Note, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.HiddenFor(m => m.UserIDCrea)
        @Html.HiddenFor(m => m.DataCrea)
        @Html.HiddenFor(m => m.UserIDModifica)
        @Html.HiddenFor(m => m.DataModifica)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Crea" class="btn btn-default" />
            </div>
        </div>
    </div>
    }

    <div>
        @Html.ActionLink("Elenco Fontane", "Indice")
    </div>
@model Centurion.Models.Fontane
@{
ViewBag.Title=“Crea”;
}
阿吉恩吉·丰塔纳
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.casimu,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.casimu,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.casimu,“,new{@class=“text danger”}) @LabelFor(model=>model.Nome,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Nome,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Nome,“,new{@class=“text danger”}) 乌比卡锡安 @LabelFor(model=>model.Indirizzo,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Indirizzo,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Indirizzo,“,new{@class=“text danger”}) @LabelFor(model=>model.Municipio,“市政”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“市政”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Municipio,“,new{@class=“text danger”}) @LabelFor(model=>model.Latitudine,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Latitudine,新 { HtmlatAttributes=新 { 占位符=“Latitudine=41,…(con la virgola)”, @class=“表单控制” } }) @Html.ValidationMessageFor(model=>model.Latitudine,“,new{@class=“text danger”}) @LabelFor(model=>model.Longitudine,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Longitudine,新 { HtmlatAttributes=新 { 占位符=“Longitudine=12,…(con la virgola)”, @class=“表单控制” } }) @Html.ValidationMessageFor(model=>model.Longitudine,“,new{@class=“text danger”}) @LabelFor(model=>model.TipoFontana,“TipoFontana”,htmlAttributes:new{@class=“control label col-md-2”}) @DropDownList(“TipoFontana”,null,htmlAttributes:new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.TipoFontana,“,new{@class=“text danger”}) @LabelFor(model=>model.TipoImpianto,“TipoImpianto”,htmlAttributes:new{@class=“controllabel col-md-2”}) @DropDownList(“TipoImpianto”,null,htmlAttributes:new{@class=“formcontrol”}) @Html.ValidationMessageFor(model=>model.TipoImpianto,“,new{@class=“text danger”}) @LabelFor(model=>model.Frequenza,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Frequenza,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Frequenza,“,new{@class=“text danger”}) @LabelFor(model=>model.Svuotamento,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Svuotamento) @Html.ValidationMessageFor(model=>model.Svuotamento,“,new{@class=“text danger”}) @Html.HiddenFor(m=>m.Active) @Html.HiddenFor(m=>m.Allarme) @LabelFor(model=>model.Foto,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Foto,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Foto,“,new{@class=“text danger”}) @LabelFor(model=>model.Allegato1,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Allegato1,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Allegato1,“,new{@class=“text danger”}) @LabelFor(model=>model.Allegato2,htmlAttributes:new{@class=“controllabel col-md-2”})
    @model Centurion.Models.Fontane

    @{
        ViewBag.Title = "Crea";
    }

    <h2>Aggiungi Fontana</h2>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.CodificaSIMU, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CodificaSIMU, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CodificaSIMU, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Nome, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Nome, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Nome, "", new { @class = "text-danger" })
            </div>
        </div>

        <fieldset>
            <legend>Ubicazione</legend>

            <div class="form-group">
                @Html.LabelFor(model => model.Indirizzo, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Indirizzo, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Indirizzo, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Municipio, "Municipio", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("Municipio", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Municipio, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Latitudine, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Latitudine, new
               {
                   htmlAttributes = new
                   {
                       placeholder = "Latitudine = 41,..... (con la virgola)",
                       @class = "form-control"
                   }
               })
                    @Html.ValidationMessageFor(model => model.Latitudine, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Longitudine, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Longitudine, new
               {
                   htmlAttributes = new
                   {
                       placeholder = "Longitudine = 12,..... (con la virgola)",
                       @class = "form-control"
                   }
               })
                    @Html.ValidationMessageFor(model => model.Longitudine, "", new { @class = "text-danger" })
                </div>
            </div>
        </fieldset>

        <div class="form-group">
            @Html.LabelFor(model => model.TipoFontana, "TipoFontana", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("TipoFontana", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TipoFontana, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TipoImpianto, "TipoImpianto", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("TipoImpianto", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TipoImpianto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Frequenza, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Frequenza, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Frequenza, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Svuotamento, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.Svuotamento)
                    @Html.ValidationMessageFor(model => model.Svuotamento, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        @Html.HiddenFor(m => m.Active)
        @Html.HiddenFor(m => m.Allarme)

        <div class="form-group">
            @Html.LabelFor(model => model.Foto, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Foto, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Foto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Allegato1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Allegato1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Allegato1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Allegato2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Allegato2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Allegato2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Note, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Note, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Note, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.HiddenFor(m => m.UserIDCrea)
        @Html.HiddenFor(m => m.DataCrea)
        @Html.HiddenFor(m => m.UserIDModifica)
        @Html.HiddenFor(m => m.DataModifica)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Crea" class="btn btn-default" />
            </div>
        </div>
    </div>
    }

    <div>
        @Html.ActionLink("Elenco Fontane", "Indice")
    </div>