BeginForm and RadioButtonFor – Session 31

BeginForm & RadioButtonFor HTML Helper - Session31

In this session of ASP.NET Core Tutorial, we continue our discussion about HTML Helper and we describe BeginForm and RadioButtonFor. During session 29 and 30, we develop a Form with HTML Helpers and in this session we improve our Form and add more features to it.

Thus, we will replace <form></form> HTML tag with BeginForm method. Then, we create a new Model for our new feature Which is Payment Method. Having said that, we reviewed creating Model, Interface and repository in session 15 and 16 and here we use those technique again. Also, we make our Radio Buttons of available Payment Method with help of RadioButtonFor.


This HTML Helper can be replaced with <form></form> HTML tag. This method of Html class has several versions of overloaded and we can select any of them which serve our requirement. Also, We can wrap BeginForm into @using and implement form element inside @using block of code. Moreover, we can set From Method through some of the overloaded versions, but by default it is POST.

Above overloaded version, contain: Action Method name, Controller name, Form Method and Html Attributes.

Payment Method Model, Interface and Repository

For the scenario of this session, we need one new Model which is PaymentMethod.

Also, in sake of implementation of Dependency Injection we need an Interface for this Model.

Next, as we have not started Database topic, we create a Static Repository of this Interface and with help of GetAll() method will return the List of Payment Method that we created in this repository.

As we mentioned in session 16, we should add this service inside ConfigureService in Startup class.

Also, we need to add PaymentMethod property to Cost Model.

PaymentMethod and list of PaymentMethod should be added to CreateCostViewModel for passing this data from/to controller/view.

Then, we can inject IPaymentMethodRepository to Cost controller to be accessible through all the Action Methods of Cost Controller.

Next, we should store sleected Payment Method and its List inside object of CreateCostViewModel. We do these changes in CreateStronglyTypedView Action Method.


For creating Radio Button in our form, we can use RadioButtonFor HTML Helper. Also, This helper has serval versions of overloaded that can serve our requirement. Moreover, for catching Payment Methods from the List we can use Foreach loop. In this example, we use ID property of PaymentMethod model as Radio Button Value and Method property of this model as Label of the Radio Button.

For changing the direction of the Radio Button in vertical position we can use <BR> tag after each Radio Button.

Also, we can use HTML Attribute for this HTML Helper that you can see its sample in below piece of code.

What is Foreach loop?

This loop is used to retrieve all the elements of a collection. Collection can be a list or array. The loop is ended when all the elements are retrieved. Also, all the element are accessible inside the loop block of code with its variable.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube. Also you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes


0 0 votes
Article Rating

Inline Feedbacks
View all comments