c#Mvc文件上传或者图片上传(预览)代码

@{
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;
        }

    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值