此方法在我的网站上出现问题。每当我尝试取消选中列表中的所有项目并离开页面并返回页面时,它仍然会被选中。我不知道发生了什么事。有人可以帮我弄这个吗?抱歉,一开始不知道如何格式化,但这是其余部分。
模型
public class CBItem
{
//Integer value of a checkbox
public int Id { get; set; }
//String name of a checkbox
public string Name { get; set; }
//Boolean value to select a checkbox
//on the list
public bool IsSelected { get; set; }
//Object of html tags to be applied
//to checkbox, e.g.:'new{tagName = "tagValue"}'
public htmlTag Tags { get; set; }
}
public class SettingsParam
{
public SettingsParam()
{
}
public LocalPasswordModel passModel { get; set; }
public CBListViewModel savedCharts { get; set; }
public CBListViewModel savedSpreads { get; set; }
public CBListViewModel savedYrSels { get; set; }
}
Controller
// POST: /Account/Manage
[HttpPost]
public ActionResult Settings(SettingsParam model, string submitBtn)
{
resetCBLists(model);
ViewBag.Message = "";
if (submitBtn.Equals("Save Email Selection"))
{
DavidChart.Models.UsersSChartContext chartCon = new DavidChart.Models.UsersSChartContext();
System.Linq.IQueryable<DavidChart.Models.UserSaveChart> userCharts = chartCon.UsersSCharts.Where<DavidChart.Models.UserSaveChart>(p => (p.UserId == WebSecurity.CurrentUserId));
CBItem selItem;
foreach (var item in userCharts)
{
if (model.savedCharts.SelectedItems == null)
{
selItem = null;
}
else
{
selItem = model.savedCharts.SelectedItems.FirstOrDefault<CBItem>(i => (i.Id == item.ChartId));
}
if (selItem != null)
{
item.SendEmail = true;
}
else
{
item.SendEmail = false;
}
}
try
{
int i = chartCon.SaveChanges();
ViewBag.Message += "Email Selection Saved";
}
catch (Exception e)
{
ViewBag.ErrorMessage = e.Message;
}
}
else if (submitBtn.Equals("Delete Custom Spread"))
{
MemoryStream stream;
BinaryFormatter bf = new BinaryFormatter();
DavidChart.Models.UserSettingsContext userSetCon = new DavidChart.Models.UserSettingsContext();
DavidChart.Models.UserSettings dbEntity = userSetCon.UserSets.FirstOrDefault(p => (p.UserId == WebSecurity.CurrentUserId));
if (dbEntity != null && !String.IsNullOrEmpty(dbEntity.SaveSpreads))
{
byte[] fromDB = Convert.FromBase64String(dbEntity.SaveSpreads);
stream = new MemoryStream(fromDB);
Models.SaveSpreads SaveS = (DavidChart.Models.SaveSpreads)bf.Deserialize(stream);
var list = new List<ValueTextPair>(SaveS.SpreadTypes);
foreach (var item in model.savedSpreads.SelectedItems)
{
var sel = list.First<ValueTextPair>(i => (i.Text.Equals(item.Name)));
list.Remove(sel);
}
SaveS.SpreadTypes = list.ToArray();
stream = new MemoryStream();
bf = new BinaryFormatter();
bf.Serialize(stream, SaveS);
byte[] bArry = stream.ToArray();
string toDBstr = Convert.ToBase64String(bArry);
dbEntity.SaveSpreads = toDBstr;
}
try
{
int i = userSetCon.SaveChanges();
ViewBag.Message += "Custom Spread Deleted";
}
catch (Exception e)
{
ViewBag.ErrorMessage = e.Message;
}
}
else if (submitBtn.Equals("Delete Year Selection"))
{
MemoryStream stream;
BinaryFormatter bf = new BinaryFormatter();
DavidChart.Models.UserSettingsContext userSetCon = new DavidChart.Models.UserSettingsContext();
DavidChart.Models.UserSettings dbEntity = userSetCon.UserSets.FirstOrDefault(p => (p.UserId == WebSecurity.CurrentUserId));
if (dbEntity != null && !String.IsNullOrEmpty(dbEntity.SaveYears))
{
byte[] fromDB = Convert.FromBase64String(dbEntity.SaveYears);
stream = new MemoryStream(fromDB);
Models.SaveYears SaveY = (DavidChart.Models.SaveYears)bf.Deserialize(stream);
var list = new List<ValueTextPair>(SaveY.YearSel);
foreach (var item in model.savedYrSels.SelectedItems)
{
var sel = list.First<ValueTextPair>(i => (i.Text.Equals(item.Name)));
list.Remove(sel);
}
SaveY.YearSel = list.ToArray();
stream = new MemoryStream();
bf = new BinaryFormatter();
bf.Serialize(stream, SaveY);
byte[] bArry = stream.ToArray();
string toDBstr = Convert.ToBase64String(bArry);
dbEntity.SaveYears = toDBstr;
}
try
{
int i = userSetCon.SaveChanges();
ViewBag.Message += "Year Selection Deleted";
}
catch (Exception e)
{
ViewBag.ErrorMessage = e.Message;
}
}
else
{
bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
ViewBag.HasLocalPassword = hasLocalAccount;
ViewBag.ReturnUrl = Url.Action("Manage");
if (hasLocalAccount)
{
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.passModel.OldPassword, model.passModel.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return RedirectToAction("Settings", new { Message = ManageMessageId.ChangePasswordSuccess });
}
else
{
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
}
}
else
{
// User does not have a local password so remove any validation errors caused by a missing
// OldPassword field
ModelState state = ModelState["OldPassword"];
if (state != null)
{
state.Errors.Clear();
}
if (ModelState.IsValid)
{
try
{
WebSecurity.CreateAccount(User.Identity.Name, model.passModel.NewPassword);
return RedirectToAction("Settings", new { Message = ManageMessageId.SetPasswordSuccess });
}
catch (Exception)
{
ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name));
}
}
}
}
resetCBLists(model);
return View(model);
}
private DavidChart.Models.ViewModels.CBListViewModel GetListViewModel(DavidChart.Models.ViewModels.PostedItems postedItems, IEnumerable<CBItem> itemList)
{
// setup properties
var model = new DavidChart.Models.ViewModels.CBListViewModel();
var selectedItems = new List<CBItem>();
var postedIds = new string[0];
if (postedItems == null)
postedItems = new PostedItems();
// if a view model array of posted ids exists
// and is not empty,save selected ids
if (postedItems.CBItemIds != null && postedItems.CBItemIds.Any())
{
postedIds = postedItems.CBItemIds;
}
// if there are any selected ids saved, create a list of CBItems
if (postedIds.Any())
{
selectedItems = itemList
.Where(x => postedIds.Any(s => x.Id.ToString().Equals(s)))
.ToList();
}
//setup a view model
model.AvailableItems = itemList.ToList();
model.SelectedItems = selectedItems;
model.PostedItems = postedItems;
return model;
}
private void resetCBLists(SettingsParam model)
{
var chartL = chartList();
var spreadL = spreadList();
var yrselL = yrselList();
if (model.savedCharts == null)
{
var cblist = new CBListViewModel();
var selectedItems = new List<CBItem>();
cblist.AvailableItems = chartList();
cblist.SelectedItems = chartSel();
model.savedCharts = cblist;
}
else
{
model.savedCharts = GetListViewModel(model.savedCharts.PostedItems, chartL);
}
if (model.savedSpreads == null)
{
var cblist = new CBListViewModel();
var selectedItems = new List<CBItem>();
cblist.AvailableItems = spreadList();
cblist.SelectedItems = selectedItems;
model.savedSpreads = cblist;
}
else
{
model.savedSpreads = GetListViewModel(model.savedSpreads.PostedItems, spreadL);
}
if (model.savedYrSels == null)
{
var cblist = new CBListViewModel();
var selectedItems = new List<CBItem>();
cblist.AvailableItems = yrselList();
cblist.SelectedItems = selectedItems;
model.savedYrSels = cblist;
}
else
{
model.savedYrSels = GetListViewModel(model.savedYrSels.PostedItems, yrselL);
}
}
查看
@Html.CheckBoxListFor(model => model.savedSpreads.PostedItems.CBItemIds,
model => model.savedSpreads.AvailableItems,
item => item.Id,
item => item.Name,
model => model.savedSpreads.SelectedItems,
MvcCheckBoxList.Model.Position.Vertical)