<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Movies.Models.StudentModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
<script type="text/javascript">
$(document).ready(function () {
//$('.date').datepicker({ dateFormat: "dd/mm/yy", Date: Date });
$('.date').datepicker();
$('.date').datepicker("setDate", new Date());
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#UImage').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function () {
readURL(this);
});
</script>
<h2>Create</h2>
<% using (Html.BeginForm("Create", "Student", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.StudentName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.StudentName) %>
<%: Html.ValidationMessageFor(model => model.StudentName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.RollNo) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.RollNo) %>
<%: Html.ValidationMessageFor(model => model.RollNo) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.STD) %>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.STD, Model.STDs, "Please Select Std", new { @class = "form-control" }) %>
<%: Html.ValidationMessageFor(model => model.STD)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Address)%>
<%: Html.ValidationMessageFor(model => model.Address)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address2) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Address2)%>
<%: Html.ValidationMessageFor(model => model.Address2)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
<input type='file' id="File1" name="File1" onchange="readURL(this)" />
<img id="UImage" src="#" height="300px" width="300px" alt="your image" />
<%: Html.ValidationMessageFor(model => model.Image)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.City) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.City)%>
<%: Html.ValidationMessageFor(model => model.City)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ZIP) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ZIP)%>
<%: Html.ValidationMessageFor(model => model.ZIP)%>
<br />
<br />
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.DOB) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.DOB, new { @class = "date" })%>
<%: Html.ValidationMessageFor(model => model.DOB)%>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</form>
</asp:Content>
这是我的 aspx 页面,我在其中使用 jquery 向用户显示上传的图像。
Controller 代码是这样的
[HttpPost]
public ActionResult Create(StudentModel StudentEn,HttpPostedFileBase File1)
{
Student st = new Student();
HttpPostedFileBase file;
file = Request.Files["File1"];///it only access the name of the object of html."file1" is the name of the object
if (file != null && file.ContentLength > 0)
{
byte[] Image=null;
Image = new byte[file.ContentLength];
file.InputStream.Read(Image, 0, file.ContentLength);
StudentEn.Image=Image;
}
if (ModelState.IsValid)
{
st.Insert(StudentEn);
return RedirectToAction("Index");
}
else
{
StudentEn.STDs = getSelectedSTD(GetSTDList());
return View(StudentEn);
}
}
这是 Controller 代码。我总是得到 request.files null 的地方。 我必须更改什么才能上传图像并将其存储到数据库中。
最佳答案
问题是您的页面中有两个表单,这令人困惑。
尝试删除/评论第一个表单
即<form id="form1" runat="server">
关于c# - Asp.net MVC 2 Request.files [""] 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485942/