peachyjumpy
peachyjumpy

Reputation: 1

What is the name/how it works this MVC characteristic?

I have used this characteristic of MVC, where you have a control in a view, lets say a checkbox:

@Html.CheckBox("isNewlyEnrolled", true)

and in the controller you declare a parameter with the same name:

[HttpPost]
public ActionResult SearchExpressionTester(string isNewlyEnrolled, SearchExpressionModel SearchExpression)

Then you can use the value of the checkbox in the controller.

Is this process of the controller receiving the string value that I don't fully understand. I know there's no need to add a new parameter to the model.

I can't find information about how this Works in any MVC book, article and so on. What is the name of this technique for passing parameters to the controler?

Upvotes: 0

Views: 61

Answers (1)

Anis Alibegić
Anis Alibegić

Reputation: 3230

Using HTML Helpers, that's all. You could also do <input type="checkbox" name="isNewlyEnrolled" checked="checked"> and the result would be the same.

Basically, you are using built-in functions to generate HTML elements.

List of most used HTML helpers:

  • Html.Beginform
  • Html.Label
  • Html.TextBox
  • Html.TextArea
  • Html.Password
  • Html.DropDownList
  • Html.CheckBox
  • Html.RadioButton
  • Html.Hidden

Html.Beginform

Usage:

@using (Html.BeginForm())
{

}

Produces:

<form action="..." action="post">

Html.Label

Usage:

@Html.Label("Name")

Produces:

<label for="Name">
    Name
</lable>

Html.TextBox

Usage:

@Html.TextBox("Name", null, null) 

Produces:

<input id="Name" name="Name" type="text" value="" />

Html.TextArea

Usage:

@Html.TextArea("Description", null, null)  

Produces:

<textarea id="Description" name="Description" rows="2" cols="20"></textarea>

Html.Password

Usage:

@Html.Password("Password")  

Produces:

<input id="Password" name="Password" type="password" value="" />

Html.DropDownList

Usage:

@Html.DropDownList("StudentGender", new SelectList(Enum.GetValues(typeof(Gender))), "Select Gender", null) 

Produces:

<select id="StudentGender" name="StudentGender">
    <option>Select Gender</option> 
    <option>Male</option> 
    <option>Female</option> 
</select>

Html.RadioButton

Usage:

@Html.RadioButton("Gender", "Male")

Produces:

<input checked="checked" id="Gender" name="Gender" type="radio" value="Male" />

Html.Hidden

Usage:

@Html.Hidden("StudentId")

Produces:

<input id="StudentId" name="StudentId" type="hidden" value="1" />

Go here to learn more are about model binding in ASP.NET.

Upvotes: 2

Related Questions