如何从ASP.NET MVC上的编辑或更新页面发布空字节

时间:2015-06-08 14:41:53

标签: c# asp.net asp.net-mvc

我正在尝试使用用户信息和图片更新页面,当我上传新图片以更新旧图片时,它会正常工作。

但是当我在没有上传图像的情况下更新页面时,它会给出一个空引用异常错误。

我想要实现的是,如果我不想更改图像,它仍应保留旧图像。

这是更新/编辑视图的操作

 [HttpPost]
    public ActionResult Edit(CombinedMember model)
    {
        try
        {
            //Updated Images

            byte[] Signature = new byte[model.MSignature.InputStream.Length];
            byte[] Photo = new byte[model.MPhoto.InputStream.Length];
            byte[] Signature2 = new byte[model.WSignature.InputStream.Length];

            model.MSignature.InputStream.Read(Signature, 0, Signature.Length);
            model.MPhoto.InputStream.Read(Photo, 0, Photo.Length);
            model.WSignature.InputStream.Read(Signature2, 0, Signature2.Length);

            WebService.UpdateMember(int.Parse(Session["memberidedit"].ToString()), model.newmember.SURNAME, model.newmember.OTHER_NAMES, model.newmember.DESIGNATION, model.newmember.ORGANISATION, model.newmember.DEPARTMENT, model.newmember.ADDRESS, model.newmember.PHONE, model.newmember.EMAIL, model.newmember.STAFFID, model.newmember.MEMBERSHIPNO, DateTime.Parse(model.newmember.DATE.ToString()), int.Parse(Session["userid"].ToString()), int.Parse(model.newmember.FLAG.ToString()),Signature,Photo);

             WebService.UpdateWitness(int.Parse(Session["witnessid"].ToString()), int.Parse(Session["memberidedit"].ToString()), model.newwitness.W_SURNAME, model.newwitness.W_OTHER_NAMES, model.newwitness.STATUS,Session["membershipno"].ToString(), Signature2);



                Session["member"] = "Details Updated successfully";
            }

        catch (Exception ex)
        {
            logger.LogError(ex.ToString());
        }

        return RedirectToAction("MembersDetails");
    }

这是上传的视图部分

<div class="form-group">
                            <label for="input-username">UPLOAD MEMBER SIGNATURE</label>
                                @Html.TextBoxFor(mw => mw.MSignature, new { type = "file" })
                            </div>
                    <div class="right" align="right">
                        @Html.Raw(TempData["mSign"])
                    </div>
                    <div class="form-group">
                        <label for="input-username">UPLOAD PHOTO</label>
                        @Html.TextBoxFor(mw => mw.MPhoto, new { type = "file" })

                        <div class="right" align="right">
                            @Html.Raw(TempData["mPhoto"])
                        </div>
                    </div>
<div class="form-group">
<label for="input-username">
UPLOAD WITNESS SIGNATURE</label>@Html.TextBoxFor(mw => mw.WSignature, new { type = "file" })
                        <div class="right" align="right">
                            @Html.Raw(TempData["wSign"])
                        </div>
                    </div>

2 个答案:

答案 0 :(得分:1)

您是否听说过空检查:

byte[] Signature;  
byte[] Photo;  
byte[] Signature2;  

if (Model.MSignature != null)
{
    Signature = new byte[model.MSignature.InputStream.Length];
    model.MSignature.InputStream.Read(Signature, 0, Signature.Length);
}

if (Model.MPhoto != null)
{
    new byte[model.MPhoto.InputStream.Length];
    model.MPhoto.InputStream.Read(Photo, 0, Photo.Length);
}

if (Model.WSignature != null) 
{
    new byte[model.WSignature.InputStream.Length];
    model.WSignature.InputStream.Read(Signature2, 0, Signature2.Length);
}

答案 1 :(得分:0)

为什么不创建另一种方法

WebService.UpdateMember(int.Parse(Session["memberidedit"].ToString()),
model.newmember.SURNAME, model.newmember.OTHER_NAMES,
model.newmember.DESIGNATION, model.newmember.ORGANISATION,  
model.newmember.DEPARTMENT, 
model.newmember.ADDRESS, model.newmember.PHONE, model.newmember.EMAIL,
model.newmember.STAFFID, model.newmember.MEMBERSHIPNO, 
DateTime.Parse(model.newmember.DATE.ToString()), 
int.Parse(Session["userid"].ToString()), 
int.Parse(model.newmember.FLAG.ToString()),Signature);

并在没有提供照片时调用