ASP.Net MVC无法编辑用户,因为od类User中的[Compare(Password)]

时间:2018-12-20 01:08:58

标签: c# asp.net-mvc

我有一个名为Korisnik的表(以我的语言,以英语为User),并且在Controller中添加了Edit ActionResult,但由于[Compare(“ Lozinka”)]正在比较来自数据库和添加的属性PotvrdiLozinku,换句话说,我必须输入确认密码才能提交更改

namespace ProjekatFinalni.Models

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public partial class Korisnik
{
    public int KorisnikID { get; set; }
    [DisplayName("Korisnicko ime:")]
    [Required(ErrorMessage ="Molimo vas unesite korisnicko ime.")]
    public string Korisnickoime { get; set; }

    [DisplayName("Lozinka:")]
    [DataType(DataType.Password)]
    [Required(ErrorMessage = "Molimo vas unesite lozinku.")]
    public string Lozinka { get; set; }

    [DisplayName("Admin:")]
    public bool DaLiJeAdmin { get; set; }
    [DisplayName("Gost:")]
    public bool Gost { get; set; }

    [DisplayName("Pravo za unos:")]
    public bool PravoUnosa { get; set; }

    [DisplayName("Potvrdi lozinku:")]
    [DataType(DataType.Password)]
    [Compare("Lozinka",ErrorMessage ="Lozinke se ne poklapaju.")]
    public string PotvrdiLozinku { get; set; }



    public string LoginErrorPoruka { get; set; }
}

这是我控制器中的Edit ActionResult

public ActionResult Edit(int id)
        {
            using (BazaProjekatEntities4 dbModel = new BazaProjekatEntities4())
            {
                return View(dbModel.Korisniks.Where(x => x.KorisnikID == id).FirstOrDefault());
            }
        }
        [HttpPost]
        public ActionResult Edit(int id,Korisnik k)
        {
            try
            {
                using (BazaProjekatEntities4 dbModel = new BazaProjekatEntities4())
                {
                    dbModel.Entry(k).State = EntityState.Modified;
                    dbModel.SaveChanges();
                }
                return RedirectToAction("Izlistaj");
             }
            catch
            {
                return View();
            }
        }

这是Edit.cshtml

@model ProjekatFinalni.Models.Korisnik

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

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

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

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

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

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

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



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



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

Only want to edit the permissions(Admin, Gost and PravoUnosa

编辑(添加了我使用的注册表)

[HttpPost]
        public ActionResult DodajiliIzmeni(Korisnik korisnikmodel)
        {
            using (BazaProjekatEntities4 Modelkorisnik = new BazaProjekatEntities4())
            {
                if(Modelkorisnik.Korisniks.Any(x=> x.Korisnickoime == korisnikmodel.Korisnickoime))
                {
                    ViewBag.DuplicateMessage = "Korisnicko ime vec postoji.";
                    return View("DodajiliIzmeni", korisnikmodel);
                }
                Modelkorisnik.Korisniks.Add(korisnikmodel);
                Modelkorisnik.SaveChanges();
            }
            ModelState.Clear();
            ViewBag.SuccessMessage = "Registracija je uspela";



            return RedirectToAction("Index", "Login");
        }

2 个答案:

答案 0 :(得分:2)

您应该创建特定于该视图的视图模型,该模型具有视图所需的属性和验证属性,并使用该模型在视图和操作方法之间传输数据。

public class EditUserVm
{
    public int Id { get; set; }

    [DisplayName("Korisnicko ime:")]
    public string UserName { get; set; }

    [DisplayName("Admin:")]
    public bool Admin { get; set; }

    [DisplayName("Gost:")]
    public bool Gost { get; set; }

    [DisplayName("Pravo za unos:")]
    public bool PravoUnosa { get; set; }
}

现在,您将在GET和POST操作方法中使用此视图模型。在您的GET操作方法中,首先创建此视图模型的对象,然后获取传入ID的Korisniks对象,读取并将属性值映射到视图模型对象,然后将其传递给视图。

public ActionResult Edit(int id)
{
  using (var dbModel = new BazaProjekatEntities4())
  {
     var user = dbModel.Korisniks.FirstOrDefault(x => x.KorisnikID == id);
     // to do: If user is NULL, return a "Not found" view to user ?

     var vm = new EditUserVm { Id = id };
     vm.UserName = user.UserName;
     vm.Admin = user.Admin;
     vm.Gost = user.Gost;
     vm.PravoUnosa = user.PravoUnosa;
     return View(vm);
  }
}

现在,确保将视图强类型化为该视图模型,因为我们正在将EditUserVm类的对象传递给它。

@model YourNamespaceGoesHere.EditUserVm
@using (Html.BeginForm())
{
  @Html.HiddenFor(a=>a.Id)
  <label>@Model.UserName</label>

  @Html.LabelFor(a=>a.Admin)
  @Html.CheckBoxFor(a=>a.Admin)

  @Html.LabelFor(a=>a.Gost)
  @Html.CheckBoxFor(a=>a.Gost)

  @Html.LabelFor(a=>a.PravoUnosa)
  @Html.CheckBoxFor(a=>a.PravoUnosa)

  <button type="submit" >Save</button>
}

现在,您将使用与操作方法参数相同的视图模型。在该方法内部,我们将再次从数据库中读取User实体,并仅udpate我们想要的字段

[HttpPost]
public ActionResult Edit(EditUserVm model)
{
   var db = new BazaProjekatEntities4();
   var user = db.Korisniks.FirstOrDefault(x => x.KorisnikID == model.Id);
   // to do : Do a null check on user to be safe :)

   // Map the property values from view model to entity object
   user.Admin = model.Admin;
   user.Gost = model.Gost;
   user.PravoUnosa = model.PravoUnosa;

   db.Entry(k).State = EntityState.Modified;
   db.SaveChanges();

   return RedirectToAction("Index");
} 

简而言之,创建一个具有视图/代码绝对需要的属性的视图模型,并使用该模型在操作方法和视图之间传输数据。

答案 1 :(得分:0)

解决方案非常简单,可能从一开始就可以,我只需要添加user.PotvrdiLozinku = user.Lozinka;它将告诉您确认密码等于密码(对于User类中的[Compare]。:)

[HttpPost]
public ActionResult Edit(EditUserVm model)
{
   var db = new BazaProjekatEntities4();
   var user = db.Korisniks.FirstOrDefault(x => x.KorisnikID == model.Id);
   // to do : Do a null check on user to be safe :)

   // Map the property values from view model to entity object
   user.Admin = model.Admin;
   user.PotvrdiLozinku = user.Lozinka;  // this line was missing
   user.Gost = model.Gost;
   user.PravoUnosa = model.PravoUnosa;

   db.Entry(k).State = EntityState.Modified;
   db.SaveChanges();

   return RedirectToAction("Index");
}