dimanche 10 janvier 2016

Why if statement always returns false in asp.net mvc when updating data?

I used this code to update user's data in asp.net mvc 5 I think every thing is OK , but I don't know why I'm getting error message and data didn't saved and if (ModelState.IsValid)is false always . Could anyone help me please ?

Admin Controller

[HttpGet]
    public ActionResult EditUser(int id)
    {

        var load = db.Users.Find(id);
        return View(load);
    }
    private const string _ImagesPathUser = "~/Images/User";

    [HttpPost]
    public ActionResult EditUser(User user, HttpPostedFileBase UploadImage)
    {
        if (ModelState.IsValid)
        {
            UserRepositories blUser = new UserRepositories();
            if (UploadImage != null)
            {
                // Delete exiting file
                System.IO.File.Delete(Path.Combine(Server.MapPath(_ImagesPathUser), user.UserImage));
                // Save new file
                string fileName = Guid.NewGuid() + Path.GetFileName(UploadImage.FileName);
                string path = Path.Combine(Server.MapPath(_ImagesPathUser), fileName);
                UploadImage.SaveAs(path);
                user.UserImage = fileName;
            }
            if (blUser.Update(user))
            {
                return JavaScript("alert(' با موفقیت ثبت شد');");
            }
            else
            {
                return JavaScript("alert('  ثبت نشد');");
            }
        }
        else
        {
            return JavaScript("alert('مقادیر  ورودی اشتباه است');");
        }
    }

UserRepositories.cs

public bool Delete(int id, bool autoSave = true)
    {
        try
        {
            var entity = db.Users.Find(id);
            db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
            if (autoSave)
            {
                bool result = Convert.ToBoolean(db.SaveChanges());
                if (result)
                {
                    try
                    {
                        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
                        {
                            File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
                        }
                    }
                    catch { }
                }
                return result;
            }
            else
                return false;
        }
        catch
        {
            return false;
        }

    }

EditUser.cs

@model NP1.Models.User

@{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}

<h2>EditUser</h2>


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

 <div class="form-horizontal">
    <h4>User</h4>
    <hr />
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.UserID)
    <div class="form-group">
        @Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserEmail)
            @Html.ValidationMessageFor(model => model.UserEmail)
        </div>
    </div>

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

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

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

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
            @Html.Upload("UploadImage")
            @Html.HiddenFor(model => model.UserImage)
        </div>
    </div>

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserGender)
            @Html.ValidationMessageFor(model => model.UserGender)
        </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>
}

Aucun commentaire:

Enregistrer un commentaire