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

Recording Node pathways that begin with the hash symbol #

How can I configure my Node/Express routing to intercept a route like /#/about and send it to /about Is there a way for node to handle routes that start with "/#"? It appears to only cut off at the hash mark. ...

Disable the shadow on the focused state of Safari and Chrome dropdown selects

I'm attempting to eliminate the shadow that appears on a select element when it is in focus: https://i.sstatic.net/5kDKE.png I have tried the following CSS styles: select { border: none; box-shadow: none; -webkit-box-shadow: none; o ...

Learn how to update a fixed value by adding the content entered into the Input textfield using Material-UI

I made a field using the Input component from material-ui: <Input placeholder="0.00" value={rate} onChange={event => { this.setState({ `obj.rate`, event.target.value }); }} /> Whenever I input a rate into this field, ...

Reduce AngularJS provider dependencies to their smallest form

I have a provider called 'securityInterceptor' implemented in my main app.js file: .provider('securityInterceptor', function() { this.$get = function($location, $q) { return function(promise) { retur ...

Is it possible for me to modify a JSON property within an OData response?

I've developed a model for use with OData, but I need to customize the property names in the JSON output. Here is my current model: public partial class Z_TESTE_DATA { [Key] public decimal DATA_ID { get; set; } public DateTime DATA_DATE ...

Having issues transferring values from one page to another. Any suggestions to make it work correctly?

I currently have two pages within my website, one is called details.page and the other is maps.page. In the details page, I have set up a search input as shown below : <form method="get" id="form-search" data-ajax="true" action="maps.page"> ...

Update content and image when clicking or hovering using Javascript

I have successfully implemented an image change on mouseover using javascript. However, I am facing a challenge in adding a description below the image upon mouseover or click. I do not have much experience with jQuery and would appreciate any help in so ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

What is the method used to calculate the heights of flex items when the flex-direction property is set to column?

I'm currently grappling with the concept of flexbox layout and have been exploring the flex-direction: column option. HTML <div class="container"> <div class="item"> Lorem ipsum dolor sit amet consectetur a ...

Having Trouble Transmitting JSON Data from C# to PHP Server

After extensive research, I am still stuck on a problem that seems to have no solution in sight. My objective is to serialize data into a JSON string and send it to a PHP file on the server. However, whenever I try to read the JSON data using json_decode, ...

developing a dynamic map with javascript

In the image provided, my concept is for it to initially be without a green tint. However, when the user hovers over specific areas, they would turn green and become clickable links. There are multiple areas in the image, with this being just one example. ...

Arranging a flex item below another flex item within a flexbox container

Is there a way to use flexbox to ensure that the 2.5 element is placed below the 2 element instead of beside it on the same row within the flex container? Given the HTML provided, how can I achieve this layout using flexbox? I attempted to accomplish thi ...

Zooming in on the background with an animated effect

I'm experimenting with a zoom in animation on a background image for a brief moment. It seems to work initially but then resets itself. To see an example, I've created a jsfiddle link: https://jsfiddle.net/onlinesolution/tk6rcLdx/ Any idea what ...

What is the best way to apply custom styles in reactJs to toggle the visibility of Google Maps?

There are three radio buttons that correspond to school, restaurant, and store. Clicking on each button should display nearby locations of the selected type. Displaying Google Map and nearby places individually works fine without any issues. class Propert ...

Achieving consistent image column heights that decrease evenly

I am trying to create a grid-like layout where each column in a row has the same height and contains an image while maintaining its proportions. Currently, I am using the flex-grow property to adjust the ratio of the images. Although this works, it often ...

Strategies for handling user typos in a JavaScript prompt

Hi there! Just wanted to say that I'm new to this website, so please forgive any posting mistakes I might make. I'm currently taking a web technology class where we're learning JavaScript. In a previous lesson, we covered HTML5 and CSS. Our ...

Transmitting a key-value pair data object to the server-side code-behind aspect

Transferring a key value pair object to the server side codebehind. What is the method to send it from JavaScript? And how can it be received in C# codebehind? Modification 1 <script type="text/javascript"> function ABC() { va ...

Discover the secret to extracting a unique style from inspect mode and incorporating it into your CSS design

I'm currently working on my angular project and I've imported the ngx-editor. My goal is to reduce the height of the editor, but I'm facing some challenges. After inspecting the element, I was able to adjust the height successfully within th ...

Moving elements upwards and downwards using CSS transitions

I am currently designing a metro tiles menu for my website, without using JavaScript and only relying on HTML and CSS. The issue I am facing involves the sliding tiles and the direction in which they slide. Additionally, there seems to be a problem where ...

What is the best way to incorporate a <c:forEach> loop into JSP operations?

I am attempting to calculate the product of two variables that are associated with an item in a loop. Below is the code snippet from the JSP file: <table> <thead> <tr> <th>PRICE</th> <th ...