4

Hi I have a the following scenario

Table - Items
    id int (PK),
    name varchar(100)

Table - Category
    id int (PK),
    name varchar(100)

Table - Category_item
    category_id int (FK),
    item_id int (FK)

I'm using Asp.Net MVC and Entitydatamodel. In the view I have a dropdown list to show the categories and a check boxes to display item.

I want to insert into the Table - Category_item when i select the category and curresponding items. How can i do it? How can I pass the values from the view to the controller?

1 Answer 1

1

The anwser is that it depends. But the general case is (RAZOR)

@Html.DropDownList(category, db.Categories.Select(cat => new SelectListItem() {Text = cat.Name, Value = cat.Id, Selected = (false/true) }).ToList());

You can use Html.DropDownListFor and then the Save action will already be bounded with correct value. 'db' is the model context.

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

2 Comments

For Items I cant use dropdown because it is a one - many relation. I'm using checkbox for showing items. How can i get those values in the view?
In that case you should use Html.CheckBox("categories", [true/false] , new { value = category.id}) for each category and on the controller side have a parameter named "categories" of type IList<[idtype]> Where idtype is the type of the id e.g. int.

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.