@{
Layout = null;
}
@model UI.Models.studentViewModle
Add
<script src="~/Conent/jquery-form.js"></script>
<script>
$(function () {
$("#fileimage").change(function () {
var reader = new FileReader();
reader.onload = function (e) {
$("#img1").prop("src",e.target.result)
}
reader.readAsDataURL(this.files[0]);
})
})
function Add()
{
if($("#form0").valid())
{
$("#form0").ajaxSubmit({
type: "post",
url: "/Index/Add",
success: function (i) {
if(i>0)
{
alert("添加成功")
location.href="/Index/Index"
}
else {
alert("添加失败")
}
}
})
}
}
</script>
<div style="width:400px; height:600px; margin:0 auto;">
@using(Ajax.BeginForm("Add","",new AjaxOptions{},new { enctype="multipart/form-data"}))
{
<p>
@Html.LabelFor(m=>m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</p>
<p>
@Html.RadioButtonFor(m => m.Sex, true, new { @checked=true})男
@Html.RadioButtonFor(m => m.Sex, false)女
</p>
<p>
@Html.LabelFor(m=>m.Age)
@Html.TextBoxFor(m => m.Age)
@Html.ValidationMessageFor(m => m.Age)
</p>
<p>
@Html.DropDownListFor(m=>m.WID,ViewBag.DropDown as List<SelectListItem>)
</p>
<p>
@Html.TextBoxFor(m => m.fileimage, new { @type="file"})
</p>
<p>
<img src="" width="80" height="80" id="img1" />
</p>
<p>
<input name="Checkbox1" type="checkbox" value="游泳" />游泳
<input name="Checkbox1" type="checkbox" value="学习" />学习
<input name="Checkbox1" type="checkbox" value="爬山" />爬山
<input name="Checkbox1" type="checkbox" value="游戏" />游戏
</p>
<p>
<input id="Submit1" type="button" value="添加" onclick="Add()" />
</p>
}
</div>
控制器代码
public int Add(studentViewModle m)
{
if(m.fileimage!=null)
{
string path = Path.Combine(Request.MapPath(“~/Image/”), m.fileimage.FileName);
m.fileimage.SaveAs(path);
m.ImageUrl = “/Image/” + m.fileimage.FileName;
}
m.fileimage = null;
m.Happy = Request.Form["Checkbox1"];
studentDataModle m1 = JsonConvert.DeserializeObject<studentDataModle>(JsonConvert.SerializeObject(m));
using(Unit3 dbe=new Unit3())
{
dbe.student.Add(m1);
int i = dbe.SaveChanges();
return i;
}
}