0

I'm trying to display a checkbox list and save the checked values to the database in MVC 2 but so far i didn't able to display the checkbox list on the view. below is the code i'm having.

ViewModel:

public class Address
    {
        public int addressID { get; set; }
        public string address { get; set; }
    }

 public class Client
    {
        public int ClientID { get; set; }
        public string Name { get; set; }
        public IEnumerable<Client> ClientOrdersGrid { get; set; }
        public List<Address> Addresses { get; set; } 
        public List<int> SelectedAddressIDs { get; set; }

    }

Controller:

public ActionResult Index()
        {
            JqGridClientRepository rep = new JqGridClientRepository();

            Address ad=new Address() {address="abcd",addressID=1};
            Address ad1=new Address() {address="kandy",addressID=2};
            Address ad2=new Address() {address="colombo",addressID=3};

            List<Address> lstAd=new List<Address>(){
                ad,ad1,ad2

            };

            var model = new Client()
            {
                Addresses = lstAd,
                ClientOrdersGrid=rep.GetClients()//This method gets an IEnumerable list of all clients

            };

            return View(model);
    }

View:

 <% foreach (var item in Model.Addresses) { %>

      // I want to display checkbox list of addresses here   

    <% } %>

1 Answer 1

0

I have modified some fields on your model like

public class Address
    {
        public int addressID { get; set; }
        public string address { get; set; }
        public bool AddreChecked { get; set; }
    }

and changed some line of code in view as

@foreach (var item in Model.Addresses)
{
    <div>&nbsp;</div> 
    @Html.CheckBox("checkboxGroup", item.AddreChecked); @item.address
}

Hope this help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.