Reputation: 562
I'm new in MVC and trying to create a site.
My main problem is that I need to activate a function in my controller after clicking a button on the view.
This is my code in the view for the button:
<input type="button" value="Add Star and Role and Categories" onclick="CreateCategoriesID"/>
On the screen in created several checkboxes from list, this is their code:
@foreach (Movies.Models.Category cat in lst)
{
@Html.CheckBox(cat.ID.ToString());
@Html.Label(cat.Name);
<br />
}
Each checkbox looks like that:
<input name="5" type="checkbox" value="true">
only their name is different.
In my controller function I need to see which checkbox is clicked, I tried that way:
public void CreateCategoriesID(object sender, EventArgs args)
{
string catID = "";
List<Category> lst = entities.Category.ToList();
foreach (Category cat in lst)
{
Request[cat.ID.ToString()].ToString();
}
}
This is the function that the button should activate.
The list is equal to the checkboxs list.
Is it the way to get elements from the screen?
How can I check if a checkbox is clicked?
How do I connect the button in the view to the function in the controller?
Thank you in advance!
Upvotes: 0
Views: 1581
Reputation: 18639
You've mixed webforms and MVC.
This:
public void CreateCategoriesID(object sender, EventArgs args)
Should be something like
public void CreateCategoriesID(SomeModel model)
{
// read categories from model, the model may be the categories themselves or a property of it dependent on your data structrue.
}
(Where the form sumbits to {Controller}/CreateCategoriesID)
For how to bind to checkbox lists -
Upvotes: 1