Ensure that the radio button is set to its default option when the user accesses the page

I have a model with a 'PartActionType' enum which includes Transfer, Harvest, and Dispose options.

    public enum PartActionType
    {
        Transfer,
        Harvest,
        Dispose
    }

On my view page, I am displaying these options using radio buttons like this:

 @foreach (var actionType in partActionTypes)
    {
      <td>
         @Html.RadioButtonFor(x => x.Components[i].SelectedActionType, actionType)
      </td>
    }

I would like to set the default option to 'Transfer'. How can I achieve this?

After following David's suggestion, this is how it looks now:

https://i.stack.imgur.com/6MjA9.png

Answer №1

It is recommended to set the default action type when initializing your view model instead of using multiple if-else statements in your view.

public ActionResult Index()
{
    var vm = new ItemViewModel
    {
        ItemId = 123,
        ItemName = "Fake Item",
        Components = new List<ItemComponentViewModel>
        {
            new ItemComponentViewModel
            {
                ComponentId = 1,
                ComponentName = "Part 1",
                SelectedActionType = PartActionType.Transfer
            },
            new ItemComponentViewModel
            {
                ComponentId = 2,
                ComponentName = "Part 2",
                SelectedActionType = PartActionType.Transfer
            },
            ...        
        };
    };

    return View(vm);
}

https://i.stack.imgur.com/wdmY6.png

Answer №2

Here is a simple solution

for each type of action in partActionTypes list
  {
    if the actionType is Transfer
      <td>
         @Html.RadioButtonFor(x => x.Components[i].SelectedActionType, actionType, new {@checked="checked"})
      </td>
    else
      <td>
         @Html.RadioButtonFor(x => x.Components[i].SelectedActionType, actionType)
      </td>
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The navigation bar options are not collapsing as intended

When I resize my page to width:750px;, my navigation bar is not collapsing the items. Despite trying to edit my CSS, I am still clueless. Here is my live page. This is the CSS for my slidebar: @media (max-width: 480px) { /* Slidebar width on extra small ...

Methods for incorporating rtl functionality into Angular Chosen

I am currently using Angular with Chosen (source) and I am attempting to implement right-to-left (RTL) support. Here is my demo, but despite referencing resources, I am encountering issues. The main problem arises when the body element has a dir="rtl" att ...

Show the cell data when the checkbox next to it is selected

I have a specific table setup and I am trying to display the content of the table cell if its corresponding checkbox is checked upon clicking a button. For example: If Row2 is checked, an alert box should display Row2 Below is my code snippet, Java ...

Desktop display issue: Fontawesome icon not appearing

Having trouble getting the fontawesome icon to display properly on my website. It appears in inspect mode, but not on the actual site itself. Any suggestions on how to fix this issue? import React, { Fragment, useState} from "react"; import { Na ...

Can the default DOM structure of a Jquery Data Table be customized to fit specific needs?

I have incorporated a Jquery Data Table Plugin that can be found at http://datatables.net/api. This plugin comes with its own search box, which is automatically added to the page when enabled. The search box is nested within its own div structure like this ...

Using JQuery to implement custom validation on a text field is an effective way

How can I validate a text field using Regex and create my own validation rule for starting with "com."? Can you provide me with an example of how to do this? I want the text field to only accept values that start with com. <input id="appPackage" name=" ...

Refresh your webpage automatically without the need to manually refresh after clicking a button using AJAX in HTML and PHP!

One issue I'm facing is that the page doesn't auto-refresh, although it loads when I manually refresh it. Below you can find my HTML and AJAX code along with its database details. The Trigger Button <?php $data = mysqli_ ...

Guide to placing a button on the same line as text with the use of JavaScript

Does anyone know how to add a button to the right of text that was added using JS DOM? I've tried multiple techniques but can't seem to get it right - the button always ends up on the next line. Any tips on how to achieve this? var text = docu ...

What could be causing this strange striping effect in the radial gradient?

What causes the striped effect in the radial gradient code provided on this website: click here to view body { background: rgba(216,215,221,1); background: -moz-radial-gradient(center, ellipse cover, rgba(enter code here216,215,221,1) 0%, rgba(0,9 ...

Having trouble advancing to the next page while attempting to web scrape

I'm currently working on scraping school data from a specific website. You can find the website here https://i.stack.imgur.com/iP37q.png. To start scraping, simply click on the "Cari Sekolah" button to navigate to the initial page. https://i.stack.i ...

Position the Bootstrip 4 tooltip to automatically display on the right side

In order to customize the placement of my tooltip on desktop, I have opted for having it positioned on the right side of the elements. While this choice aligns well with my design preferences, it has presented a challenge when viewing the page on smaller s ...

The simple method for incorporating line feed in <s:textarea>

Hey there, I'm currently utilizing struts 2 UI and I seek a means to restrict the number of characters in each row to only 16. Additionally, I want to impose a maximum length limit of 32 characters. Despite my attempts using the 'row' and &a ...

IntelliJ automation in Java does not allow clicking the button

Currently, I am on the PayPal page and I need to automate clicking the agree button. <form id="ryiForm" class="proceed ng-pristine ng-valid" ng-class="{true: 'validated'}[validated]" ng-submit="confirm.$valid && onPay()" novalidate= ...

Is there a way to make a Bootstrap grid with four columns span across two rows in the second column?

How can I create a Bootstrap grid with four columns where the second column spans two rows? Hello, I am looking for help to achieve a portfolio grid layout with 4 columns (refer image here) and have the second column span 2 rows. I plan to include images ...

Having trouble with overriding an @media query for material-ui react components?

I've been trying to modify a @media CSS rule on a Material UI component, similar to the discussions on How to over-ride an @media css for a material-ui react component and Override components like MuiTab that use media queries. However, I have not bee ...

Looking for text within a list with similar paths - a guide

Recently, I delved into learning about selenium and c#. I am facing an issue in my project where items are being added to the cart. The challenge is to verify that they have been successfully added. I have attempted to use XPath, CssSelector, and ClassNam ...

Challenges with basic contact form

Apologies in advance for any beginner mistakes, as I am venturing into creating an HTML/PHP contact form for the first time. Despite researching similar problems faced by others, I have not come across a solution. The main issue is that the email is not b ...

Discover the art of customizing child elements using vanilla extract!

I recently started using vanilla extract to add styles to a NextJS application. Is there a way to style child elements within the parent element without having to create another class? My React component has a structure like this: <ul className={style ...

Conceal the div by clicking outside of it

Is there a way to conceal the hidden div with the "hidden" class? I'd like for it to slide out when the user clicks outside of the hidden div. HTML <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c ...

VS code is showing the directory listing instead of serving the HTML file

Recently, I attempted to use nodejs to serve the Disp.html file by following a code snippet from a tutorial I found on YouTube. const http = require("http"); const fs = require("fs"); const fileContent = fs.readFileSync("Disp.html& ...