HTTPRequest连接以前的值

时间:2014-11-06 12:00:50

标签: c# html asp.net-mvc http-post html-form-post

我有一个局部视图,将表单传递给MVC控制器(checkBox.Checked值列表是特定的)。问题是,每次下次调用此视图时,新值都会连接到旧值。例如,第一次值为true,第二次为true,为true,第三次为true,true,true等。我怎么能避免这个?

以下是部分视图的代码:

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

}

以下是MVC操作的代码:

    [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);

    }

0 个答案:

没有答案