0

I have a partial view, that passes form to MVC controller (list of checkBox.Checked values to be specific). The problem is that each next time I call this view, new values are getting concatenated to old ones. For example, first time the valu will be true, the second time - true,true, the third time - true,true,true and so on. How can I avoid this?

Here is the code for partial view:

@using (Html.BeginForm("SaveGridSettings", "ExtendedGrid", FormMethod.Post))
{
    <div class="alert alert-warning">

        @Html.Hidden("objectType", (string)ViewBag.GridType)
        @Html.Hidden("userID", (string)ViewBag.UserID)
        <table width="100%" border="1">
            @{
                using (var context = new EfDbContext())
                {
                    var cellsInRow = 10;
                    var i = 0;
                    while (i < ViewBag.PropertiesList.Length)
                    {
                        <tr>
                            @{ int fin = ViewBag.PropertiesList.Length - i < cellsInRow ? ViewBag.PropertiesList.Length - i : cellsInRow; }
                            @for (var j = 0; j < fin; i++)
                            {
                                string propertyName = ViewBag.PropertiesList[i];//.Name;

                                var columnInfo = context.ExtendedGridColumnsViewSettings.AsEnumerable().FirstOrDefault(it => it.TypeName.Equals(ViewBag.GridType) &&
                                                                                                                             it.FieldName.Equals(propertyName) &&
                                                                                                                             it.UserID.Equals(ViewBag.UserID));
                                var isChecked = columnInfo == null || columnInfo.IsChecked;

                                //if (ViewBag.PropertiesList[i].PropertyType.GetInterface("ICollection") == null)
                                //{
                                    <td align ="center">
                                        @Html.Label(propertyName)
                                        @Html.CheckBox(propertyName, isChecked)
                                        @{
                                            j++;
                                        }
                                    </td>
                                //}

                            }
                        </tr>
                    }
                }
            }
        </table>
        <br><br>
        <center>
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="submit" id="approve-btn" class="btn btn-danger">Save</button>
        </center>
    </div>

}

Here is the code for MVC action:

    [HttpPost]
    public ActionResult SaveGridSettings()
    {
        string settings = "";

        var objectType = Request["objectType"];
        var userID = Request["userID"];

        var listOfProperties = Request.Form.AllKeys.Where(it => !it.Equals("objectType") && !it.Equals("userID"));

        using (var context = new EfDbContext())
        {
            foreach (var property in listOfProperties)
            {
                if (String.IsNullOrEmpty(Request[property])) continue;

                var isChecked =
                    Boolean.Parse(Request[property]); //HERE THE VALUE COMES CONCATENATED

                var existingSetting =
                    context.ExtendedGridColumnsViewSettings.AsEnumerable()
                        .FirstOrDefault(
                            it =>
                                it.TypeName.Equals(objectType) && it.FieldName.Equals(property) &&
                                it.UserID.Equals(userID));
                if (existingSetting != null)
                {
                    existingSetting.IsChecked = isChecked;
                    context.ExtendedGridColumnsViewSettings.Attach(existingSetting);
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(existingSetting, EntityState.Modified);
                }
                else
                {
                    existingSetting = new ExtendedGridColumnsViewSetting
                    {
                        FieldName = property,
                        TypeName = objectType,
                        UserID = userID,
                        IsChecked = isChecked
                    };
                    context.ExtendedGridColumnsViewSettings.Add(existingSetting);
                }

            }
            context.SaveChanges();
        }

        var url = Request.UrlReferrer.AbsolutePath;
        return Redirect(url);

    }
KorsaR
  • 536
  • 1
  • 10
  • 26
  • I think this is bootstrap modal popup. And when you close it you do not destroy it. So, Every time you open a new modal and post data, it also post data of existing check-boxes of previous modal. This might be an issue. It is just a guess of what may be happening at your side. – Ankush Jain Nov 06 '14 at 12:40
  • @AnkushJain, yes, it is bootstrap modal popup. And how should I correctly destroy it? – KorsaR Nov 06 '14 at 13:38
  • $('#myModal').on('hidden.bs.modal', function () { // clear the content of modal popup when popup has been hidden in this event }) – Ankush Jain Nov 06 '14 at 18:51
  • http://stackoverflow.com/questions/12319171/how-to-handle-the-modal-closing-event-in-twitter-bootstrap – Ankush Jain Nov 06 '14 at 18:52

0 Answers0