2017-02-10 73 views
1

嘿傢伙我編碼在MVC 5代碼第一現在我有這張表下面,當我想編輯單元格編號或電子郵件和我的更改都保存在數據庫中我的圖片被刪除我不知道爲什麼,因爲我沒有改變它。每次我編輯等信息的圖片得到了刪除當我保存我的變化MVC 5代碼第一編輯

[Key] 
public int Member_Id { get; set; } 
[Required] 
[StringLength(20, MinimumLength = 3, ErrorMessage = "Please enter minimum of 3 characters")] 
[RegularExpression(@"^[a-zA-Z\s?]+$", ErrorMessage = "Pease enter valid name!")] 
[Display(Name = "First Name")] 
public string Name { get; set; } 
[Required] 
[RegularExpression(@"^[a-zA-Z\s?]+$", ErrorMessage = "Pease enter valid Surname!")] 
[StringLength(20, MinimumLength = 3, ErrorMessage = "Please enter minimum of 3 characters")] 
[Display(Name = "Surname")] 
public string Surname { get; set; } 
[Required] 
[RegularExpression(@"^(\d{10})$", ErrorMessage = "Cellphone number must be 10 digits!")] 
[Display(Name = "Cell Number")] 
public string Cell_Number { get; set; } 
[Required] 
[RegularExpression(@"^(\d{13})$", ErrorMessage = "id must be 13 digits!")] 
[Display(Name = "ID Number")] 
public string ID_Number { get; set; } 
[Required] 
[RegularExpression(".+\\@.+\\..+", ErrorMessage = "Please enter a valid email address")] 
[Display(Name = "Email Address")] 
public string Email { get; set; } 
[Required] 
[Display(Name = "Physical Address")] 
public string Address { get; set; } 
[Display(Name = "Owner")] 
public bool Owner { get; set; } 
[Display(Name = "Driver")] 
public bool Driver { get; set; } 
[Display(Name = "Rank Manager")] 
public bool Rank_Manager { get; set; } 
[Display(Name = "Profile Picture")] 
public byte[] Picture { get; set; } 
public string alter_Text { get; set; } 
public string ImageMimeType { get; set; } 
public virtual ICollection<Taxi> Taxi { get; set; } 

,這是我的控制器

public ActionResult EditPick(int? id) 
{ 
    if (id == null) 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
    } 
    Member member = db.Member.Find(id); 
    if (member == null) 
    { 
     return HttpNotFound(); 
    } 
    return View(member); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult EditPick(/*[Bind(Include = "Member_Id,Name,Surname,Cell_Number,ID_Number,Email,Address,Owner,Driver,Rank_Manager,Picture,alter_Text,ImageMimeType")]*/ Member member,HttpPostedFileBase image) 
{ 
    try 
    { 

      var picturee = new Member(); 

      if (image != null) 
      { 
       if (image.ContentLength > 2 * 1024 * 1024) 
       { 
        ModelState.AddModelError("CustomError", "The File size be not more than 2 MB"); 
        return View(); 
       } 
       if (!(image.ContentType == "image/jpeg" || image.ContentType == "image/gif")) 
       { 
        ModelState.AddModelError("CustomError", "The File Allowed : jpeg and gif"); 
        return View(); 

       } 
       if (image != null) 
       { 
        member.ImageMimeType = image.ContentType; 
        member.Picture = new byte[image.ContentLength]; 
        image.InputStream.Read(member.Picture, 0, image.ContentLength); 
       } 
      } 
      picturee.Member_Id = member.Member_Id; 
      picturee.Name = member.Name; 
      picturee.Surname = member.Surname; 
      picturee.Cell_Number = member.Cell_Number; 
      picturee.ID_Number = member.ID_Number; 
      picturee.Email = member.Email; 
      picturee.Address = member.Address; 
      picturee.Owner = member.Owner; 
      picturee.Driver = member.Driver; 
      picturee.Rank_Manager = member.Rank_Manager; 
      picturee.Picture = member.Picture; 
      picturee.ImageMimeType = member.ImageMimeType; 
      picturee.alter_Text = member.alter_Text; 



      db.Entry(picturee).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 

     } 
     catch (DataException) 
    { 

     ModelState.AddModelError("", "Sorry can not update contact Administrator"); 


    } 

    return View(member); 
} 

這是我發佈

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Member</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.Member_Id) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Cell_Number, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Cell_Number, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Cell_Number, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ID_Number, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ID_Number, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ID_Number, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Owner, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <div class="checkbox"> 
        @Html.EditorFor(model => model.Owner) 
        @Html.ValidationMessageFor(model => model.Owner, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Driver, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <div class="checkbox"> 
        @Html.EditorFor(model => model.Driver) 
        @Html.ValidationMessageFor(model => model.Driver, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Rank_Manager, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <div class="checkbox"> 
        @Html.EditorFor(model => model.Rank_Manager) 
        @Html.ValidationMessageFor(model => model.Rank_Manager, "", new { @class = "text-danger" }) 
       </div> 
      </div> 
     </div> 
     <div class="form-group"> 
     @Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @{ 
    if (Model.Picture != null) 
    { 
     string imageBase64 = Convert.ToBase64String(Model.Picture); 
     string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64); 
     <img src="@imageSrc" width="100" height="100" /> 
    } 
      } 
      @Html.ValidationMessageFor(model => model.Picture, "", new { @class = "text-danger" }) 
     </div> 
    </div> 


     <div class="form-group"> 
      @Html.LabelFor(model => model.alter_Text, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.alter_Text, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.alter_Text, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.ImageMimeType, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.ImageMimeType, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.ImageMimeType, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
+0

請顯示查看cshtml。特別是你如何發佈表單。 – Steve

+0

@Steve我已添加我的視圖,請幫助 – Sibonelo

+0

要將文件發佈到您的控制器,您需要具有名稱=圖像的Input = File您在視圖中顯示圖像的事實並不意味着該視圖可以發佈它回來作爲一個文件 – Steve

回答

0

如說,當查看評論,您需要在您的視圖中添加一個Input=File Name=Image以將文件與您對個人資料圖片的更改一起回傳,但最重要的是要回讀會員信息在你的數據庫中,不建立一個新的成員實例。
實際上,如果視圖不回發文件,並且當您保存圖像字節時,新成員對前一張圖像沒有任何信息。

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult EditPick(Member member,HttpPostedFileBase image) 
{ 
    try 
    { 
     var picturee = new context.Member.Find(member.Member_Id); 
     if(picturee != null) 
     { 
      ..... 
     } 
     // Now you can start updating the other fields and then save. 
     // This is not needed -> picturee.Member_Id = member.Member_Id; 
     picturee.Name = member.Name; 
     picturee.Surname = member.Surname; 
     .... 

通過這種方式重新加載圖像字節情況下查看不回發一個文件的讀取

在視圖中,您需要添加appriate代碼發送回簡介圖像,如果用戶希望更新。

<div class="form-group"> 
    @Html.LabelFor(model => model.Picture, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
      <input name="Image" type="file" /> 
    </div> 
    @{ 
     if (Model.Picture != null) 
     { 
      string imageBase64 = Convert.ToBase64String(Model.Picture); 
      string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64); 
      <img src="@imageSrc" width="100" height="100" /> 
     } 
    } 
    @Html.ValidationMessageFor(model => model.Picture, "", new { @class = "text-danger" }) 

</div> 
+0

非常感謝你史蒂夫,它現在工作得很好 – Sibonelo