<%@ 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向用户显示上传的图像。
控制器代码类似于
[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);
}
}
这是控制器代码。我总是得到request.files null。 我需要更改以上传图像并将其存储到数据库中。
答案 0 :(得分:0)
修改剃刀代码如下
尝试下面的剃刀代码
<% = Html.BeginForm("Create", "Student", FormMethod.Post, new { enctype = "multipart/form-data" })%>
...............
..............
<% Html.EndForm(); %>
删除using
并尝试
尝试下面的代码..使用File1直接分配..它适用于我
[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 (File1 != null && File1.ContentLength > 0)
{
byte[] Image=null;
Image = new byte[File1.ContentLength];
File1.InputStream.Read(Image, 0, File1.ContentLength);
StudentEn.Image=Image;
}
if (ModelState.IsValid)
{
st.Insert(StudentEn);
return RedirectToAction("Index");
}
else
{
StudentEn.STDs = getSelectedSTD(GetSTDList());
return View(StudentEn);
}
}
答案 1 :(得分:0)
试试这个:
public ActionResult Create(StudentModel StudentEn,FormCollection formCollection)
{
HttpPostedFileBase file= formCollection.Get("File1");
}
答案 2 :(得分:0)
问题是您在页面中有两个表单令人困惑。
尝试删除/评论第一个表单
即。 <form id="form1" runat="server">