0

我在编辑表单中有这样的代码。

@using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<img id="sp" src="/Student/RetrieveImage/model.StudentID" alt="Photo" height=100 width=200 />
<input type="file" name="ImageData" id="ImageData" onchange="DI();"/>
}

这在学生控制器中

HttpPostedFileBase file;
public ActionResult Bind()
{
    file = Request.Files["ImageData"];
    file = Request.Files["ImageData"];
    return RedirectToAction("StudentEdit");
}

开始表单在编辑表单中找到,它是一个部分表单。我需要的是从输入文件中选择文件时触发绑定操作。我怎样才能做到这一点?

4

1 回答 1

2

您正在使用表单(html 元素)来上传图像(使用 HTTP POST 动词),在您的示例中,您有两个主要选项可以继续上传:

第一个选项:在表单内创建提交输入

@using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="ImageData" id="ImageData" onchange="DI();"/>
    <input type="submit" name="submitbutton" value="Upload" />
}

第二个选项:使用 javascript 提交表单,在此示例中,我使用了您在文件元素中创建的事件:

@using (Html.BeginForm("Bind", "Student", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="ImageData" id="ImageData"  onchange="this.form.submit();"/>
}
于 2016-06-14T20:58:58.450 回答