0

I'm trying to understand MVC.

Let's say on the View I have a checkbox, we'll call it 'checkBox1'...

In my Controller, can I access this checkbox directly?

Can I go: checkBox1.Checked = true

??

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • Functionally, you shouldn't. Technically, it depends on the MVC framework used (which you didn't mention at all in your question). For example JSF allows binding the view component to the controller class. But this is usually used for other purposes than getting/setting the value, it's also often considered poor practice. Getting/setting the value is supposed to go through the model. – BalusC Aug 25 '11 at 22:56

2 Answers2

3

A controller cannot directly access the elements of a view as it has no information about it. A controller can only act as a point of control to regulate data between the views and models. However you can manipulate the views from the controller like loading a specific view template or rendering a chunk of code ( in case of xhr request) to the output. But once it renders that it will not have any information about the individual elements of the view.

Take a look at this

http://www.enode.com/x/markup/tutorial/mvc.html

swordfish
  • 4,899
  • 5
  • 33
  • 61
1

No. The controller deals with the data (model) sent to and from the view and is deliberately separated from the details of the View.

I suggest spending some time reading up on the basics of ASP.NET MVC and doing some tutorials on the ASP.NET MVC site

Read here for details on handling checkboxes... CheckboxList in MVC3.0

Community
  • 1
  • 1
brodie
  • 5,354
  • 4
  • 33
  • 28