C# 如何使用ADO.NET在一个表单内使用MVC3将图像上载到SQL?

C# 如何使用ADO.NET在一个表单内使用MVC3将图像上载到SQL?,c#,sql,asp.net-mvc-3,ado.net,razor,C#,Sql,Asp.net Mvc 3,Ado.net,Razor,我在将图像上传字段放入字段集中的其余部分时遇到问题,主要是因为我需要对一个表单执行两个操作,如果我错了,请纠正我!因此,当使用模型呈现视图中的字段时,没有@Html.UploadField或类似的。。。。有什么想法吗 我的目标是将图像与表单的其余部分保存在同一行中 型号: public class MyModel { [HiddenInput(DisplayValue = false)] public int ProductID { get; set; } [Dis

我在将图像上传字段放入字段集中的其余部分时遇到问题,主要是因为我需要对一个表单执行两个操作,如果我错了,请纠正我!因此,当使用模型呈现视图中的字段时,没有@Html.UploadField或类似的。。。。有什么想法吗

我的目标是将图像与表单的其余部分保存在同一行中

型号:

 public class MyModel
 {
    [HiddenInput(DisplayValue = false)]
    public int ProductID { get; set; }

    [Display(Name = "Author: ")]
    public string Author { get; set; }

    [Display(Name = "Title: ")]
    public string Title { get; set; }

    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }

}
@model namespace.MyModel

@using (Html.BeginForm("AddForm", "Admin"))
{

<div>
    <fieldset>
        <legend>New Article</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.Author)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Author)
            @Html.ValidationMessageFor(m => m.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div> 
         <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
        </div>

         @using (Html.BeginForm("SaveImage", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {   
            <div class="editor-label">Image</div>
            <div class="editor-field">
                <div>Upload image: <input type="file" name="Image" /></div>
            </div>

        }

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</div>
}
查看:

 public class MyModel
 {
    [HiddenInput(DisplayValue = false)]
    public int ProductID { get; set; }

    [Display(Name = "Author: ")]
    public string Author { get; set; }

    [Display(Name = "Title: ")]
    public string Title { get; set; }

    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }

}
@model namespace.MyModel

@using (Html.BeginForm("AddForm", "Admin"))
{

<div>
    <fieldset>
        <legend>New Article</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.Author)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Author)
            @Html.ValidationMessageFor(m => m.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div> 
         <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
        </div>

         @using (Html.BeginForm("SaveImage", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {   
            <div class="editor-label">Image</div>
            <div class="editor-field">
                <div>Upload image: <input type="file" name="Image" /></div>
            </div>

        }

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</div>
}

我的问题是将这两种方法结合在一起,并查看本地MVC3 uploadfield

在viewmodel中,将图像字段声明为HttpPostedFileBase,如下所示:

public HttpPostedFileBase ImageData { get; set; }
在视图中,确保文件输入的ID和名称属性都与viewmodel上的属性名称对应:

<input type="file" name="ImageData" id="ImageData" />

提交表单时,上载的文件将由默认模型绑定器填充到HttpPostedFileBase属性中

此外,您的Html.Form需要包装所有输入字段,而不仅仅是文件上载。嵌套表单在HTML中无效

@using (Html.BeginForm("AddForm", "Admin", FormMethod.Post, 
    new { enctype = "multipart/form-data" }))
{
<div>
    <fieldset>
        <legend>New Article</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.Author)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.Author)
            @Html.ValidationMessageFor(m => m.Author)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Title)
        </div> 
         <div class="editor-field">
            @Html.TextBoxFor(m => m.Title)
        </div>  
        <div class="editor-label">Image</div>
        <div class="editor-field">
            <div>Upload image: 
                <input type="file" name="ImageData" id="ImageData" />
            </div>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</div>
}
@使用(Html.BeginForm(“AddForm”、“Admin”、FormMethod.Post、,
新的{enctype=“multipart/form data”})
{
新文章
@LabelFor(m=>m.Author)
@Html.TextBoxFor(m=>m.Author)
@Html.ValidationMessageFor(m=>m.Author)
@LabelFor(m=>m.Title)
@Html.TextBoxFor(m=>m.Title)
形象
上载图像:

}
更新


我无法回答问题的ADO.NET部分。我们使用EF4.1和上述方法,通过实体属性保存到数据库表中。EF为我们处理ADO.NET部分,所以我不确定使用裸ADO.NET的代码是什么

@oliverhour-谢谢,你能解释一下如何用ado.net以一种方法保存这个,包括上传的图片吗?