开发者

ASP.NET MVC and LINQ to SQL: linking table, checkboxes

开发者 https://www.devze.com 2022-12-21 15:11 出处:网络
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 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号