0

I am using DevExpress GridView Popup Edit Form. In my model Password and ConfirmPassword fields are required. When user Edit a record, I am using below given markup to mark controls as visible or invisible as admin user is not allowed to change password once user is created, but my modelState is still invalid and says Password is required in edit mode even it is invisible.

I am using MVC Extensions 19.1

        Html.DevExpress().FormLayout(settings =>
        {
            var rlUserId = Convert.ToInt32(DataBinder.Eval(c.DataItem, "RLUserId"));
            settings.Name = "FormLayoutRLUsers";
            settings.Width = Unit.Percentage(100);
            settings.SettingsAdaptivity.AdaptivityMode = 
            FormLayoutAdaptivityMode.SingleColumnWindowLimit;
            settings.SettingsAdaptivity.SwitchToSingleColumnAtWindowInnerWidth = 700;
            settings.ColCount = 2;

            settings.Items.Add(i =>
            {
                i.FieldName = "LastName";
                i.NestedExtension().TextBox(tx =>
                {
                    AssignTextBoxSettings(tx);
                });
            });
            settings.Items.Add(i =>
            {
                i.FieldName = "FirstName";
                i.NestedExtension().TextBox(tx =>
                {
                    AssignTextBoxSettings(tx);
                });
            });               
           settings.Items.Add(i =>
            {
                i.FieldName = "Password";
                i.NestedExtension().TextBox(tx =>
                {
                    tx.Properties.Password = true;
                    if (rlUserId > 0)
                    {
                        i.Visible = false;
                        tx.ClientVisible = false;
                    }
                    else
                    {
                        i.Visible = true;
                        AssignTextBoxSettings(tx);
                        tx.ClientVisible = true;
                    }
                });
            });
            settings.Items.Add(i =>
            {
                i.FieldName = "ConfirmPassword";
                i.NestedExtension().TextBox(tx =>
                {
                    tx.Properties.Password = true;
                    if (rlUserId > 0)
                    {
                        i.Visible = false;
                        tx.ClientVisible = false;
                    }
                    else
                    {
                        i.Visible = true;
                        AssignTextBoxSettings(tx);
                        tx.ClientVisible = true;                       
                    }
                });
            });​                
            settings.Items.Add(i =>
            {
                i.ShowCaption = DefaultBoolean.False;
            }).SetNestedContent(() =>
            {
                ViewContext.Writer.Write("<div style='float:right'>");
                Html.DevExpress().Button(btnSettings =>
                {
                    btnSettings.Name = "btnUpdate";
                    btnSettings.Text = "Update";
                    btnSettings.ClientSideEvents.Click = "function(s, e){ gridView.UpdateEdit(); }";
                }).Render();
                Html.DevExpress().Button(btnSettings =>
                {
                    btnSettings.Name = "btnCancel";
                    btnSettings.Text = "Cancel";
                    btnSettings.Style[HtmlTextWriterStyle.MarginLeft] = "5px";
                    btnSettings.ClientSideEvents.Click = "function(s, e){ gridView.CancelEdit(); }";
                }).Render();
                ViewContext.Writer.Write("</div>");
            });
        }).Bind(ViewData["EditableUser"] ?? GetEditingUser(c.Grid))
        .Render();
Zafar
  • 441
  • 4
  • 9

1 Answers1

0

there can be many solutions like :

  1. Before checking the model state you can remove the field like this ModelState.Remove("Password").

  2. you can make this password field based on role or based on the page user is coming. you can send/store the values in view bag.

  3. At the time of update, you can make the password field read-only or disabled.

There is various demo already given in devexpress website to read this kind of basic thing. Please have a look there.

Manohar Thakur
  • 313
  • 1
  • 2
  • 9