tags:

views:

34

answers:

2

I am using a checkboxlist helper and dynamically binding it..Now i want to maintain the state of checkboxes?

 public ActionResult Step3()
        {
            CustomerQuestion _Cust = new CustomerQuestion();
            //Retrieve the answer from database by siteid
            var Questions = QAService.GetAllAnswer(1);
            var Questionscount = QAService.GetAllAnswer(1).Count();
            _Cust.Question18 = Questions.Where(s => s.QuestionID == 18);
            return View(_Cust);
        }

and here is the view

 <%= Html.CheckBoxList("Question18", new SelectList(Model.Question18, "AnswerID", "Answer"))%>
A: 

show the code so we can help you out.

I guess you want to maintain the checkboxes when you return the view and there is formvalidation.

did you return your object in the view?

EDIT:

You cant make more than 1 value selected in a SelectList. Better is to use List<SelectListItem>

I use this code.

    public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty, Func<T, string> valueProperty, Predicate<T> isSelected) {
        var result = new List<SelectListItem>();

        foreach (var item in itemsToMap) {
            result.Add(new SelectListItem {
                Value = valueProperty(item),
                Text = textProperty(item),
                Selected = isSelected(item)
            });
        }
        return result;
    }

then

ViewData["test"]= _Cust.Question18.ToSelectList(q=>q.Answer, q=>q.AnswerID, q=>someListOfAnswerIDS.Contains(q.AnswerID));


<%= Html.CheckBoxList("Question18", ViewData["test"] as List<SelectListItem>)%>
Stefanvds
But I want to continue with the checkboxlist helper ...n that is what i have to use strictly
Hasan
check the edit...
Stefanvds
A: 

public ActionResult Step3() { CustomerQuestion _Cust = new CustomerQuestion(); //Retrieve the answer from database by siteid var Questions = QAService.GetAllAnswer(1); var Questionscount = QAService.GetAllAnswer(1).Count(); _Cust.Question18 = Questions.Where(s => s.QuestionID == 18); return View(_Cust); }

and here is the view

<%= Html.CheckBoxList("Question18", new SelectList(Model.Question18, "AnswerID", "Answer"))%>

i am binding all the languages from the database in this list. if there is some validation error on this page than i want to maintain the check state of the check boxes

Hasan