【问题标题】:MVC 5 Code First EditingMVC 5 代码优先编辑
【发布时间】:2017-02-10 10:35:38
【问题描述】:

嘿,伙计们,我首先使用 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>

【问题讨论】:

  • 请显示视图cshtml。尤其是您如何发布表单。
  • @Steve 我已经添加了我的视图,请帮忙
  • 要将文件发布到您的控制器,您需要具有 Name=Image 的 Input=File 您在视图上显示图像的事实并不意味着视图可以将其作为文件

标签: asp.net-mvc html character


【解决方案1】:

正如 cmets 中所说,您需要在视图中添加 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;
        ....

通过这种方式,您可以重新加载图像字节,以防视图不回发文件进行读取

如果用户希望更新个人资料图片,您需要在视图中添加适当的代码以发回个人资料图片。

<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>

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多