views:

316

answers:

1

I am very new to MVC and from what I learned, I can have a form (a View) linked to a Model.

Now I have a View for adding a new Client, so the View inherits from Client.
I have something like this for each field

  <%= Html.LabelFor(model => model.FirstName) %>
  <%= Html.TextBoxFor(model => model.FirstName)%>
  <%= Html.ValidationMessageFor(model => model.FirstName)%>

No problem with that, it can be saved to the database without any SQL statements thanks to LINQ.

Now the problem is, a Client can belong to Group(s)
(the relation between Clients and Groups is many to many)

I have created a linking table called ClientGroups that has 2 columns:
ClientID
GroupID

Ideally the form should have a checkbox list, so I am wondering if something similar to this can be done

  <label>Groups</label>
  <%= Html.CheckBoxListFor(model => model.Groups)%>

That won't compile, but what is the proper way of doing it?

Thanks in advance!

A: 

i'd create GroupViewModel and use it instead of Group; GroupViewModel should contain something like NamedValueCollection with entities - then you should be able to use it in HtmlHelpers

Jack
Thanks Jack... How can I use HtmlHelpers to create the checkboxes?
for example: <%= Html.CheckBox("MyCheckBox") %> you should try some other overloaded helper for checkbox, there are more parameters to set
Jack