AngularJS Dropdown in ASP.NET MVC 3

I want to create a DropDownList using @Html.DropDownList and connect it with AngularJS.

Below is the code snippet:

@Html.DropDownList("LessonID", (SelectList)ViewBag.LessonList, "choose one", new { ng_model = "LessonID" })
    

Here's an example of how the data should look like: [LessonID:1, LessonName:English][LessonID:2, LessonName:Sport]......

Then the corresponding html code would be:

<select id="LessonID" class="ng-pristine ng-valid" ng-model="LessonID" name="LessonID">
        <option value="? object:null ?"></option>
        <option value="">choose one</option>
        <option value="1">English</option>
        <option value="2">Sport</option>
    

How can we make sure that the first option is not shown?

Is it possible to remove it?

Answer №1

To ensure the first option is removed, make sure to assign a default value to your scope variable in the controller. In this case, you have named your scope variable LessionID which is bound to your dropdown list. It seems that you may have forgotten to set $scope.LessionID to a default value, resulting in an undefined entry being displayed in the dropdown.

Check out the code snippet below:

function LessionCtrl($scope) {
  $scope.LessionID = '';

  $scope.alertLession = function() {
    alert($scope.LessionID);
  };
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app>
  <h2>Lession</h2>
  <div ng-controller="LessionCtrl">
    <select id="LessionID" ng-model="LessionID" ng-change="alertLession()" name="LessionID">
      <option value="">chose one</option>
      <option value="1">English</option>
      <option value="2">Sport</option>
    </select>
  </div>

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

Should arrays of objects be kept in databases or JavaScript files?

Here is a small excerpt from an array of objects utilized on my website. It's just a snippet and not the full JS file. I have several JS files like this, some reaching up to 500 lines of code. Currently delving into databases ...

How does Angular Focus Manager align with WAI-ARIA standards for accessibility?

Someone please explain ARIA compatibility of Angular Focus Manager that is mentioned on this Github page. ...

Eliminating bottom section in HTML/CSS

I've got this code snippet: new WOW().init(); /* AUTHOR LINK */ $('.about-me-img img, .authorWindowWrapper').hover(function() { $('.authorWindowWrapper').stop().fadeIn('fast').find('p').addClass('tr ...

Is there a way to generate a hierarchical list menu using strings?

Within an HTML Application (HTA), I am utilizing vbscript to retrieve a list of subnet locations which is output as text in the following format: Chicago Denver Dallas Dallas/North Dallas/South Dallas/West Dallas/West/Building1 Dallas/West/Bu ...

Is there a way to programmatically prevent the back button from functioning if the previous route pathname in React was 'Login'?

When it comes to navigating back on previous pages, the traditional back button is typically used instead of relying solely on the navigation bar. However, I am currently looking to disable this feature specifically when the next previous route in line is ...

What is preventing my function from retrieving user input from an ngForm?

I'm currently working on my angular component's class. I am attempting to gather user input from a form and create an array of words from that input. The "data" parameter in my submit function is the 'value' attribute of the ngForm. Fo ...

When using PHP's `json_encode()`, don't forget to append a "1" at the

While utilizing json_encode in my project, I have encountered an issue that is perplexing. On one particular page where I make an ajax call, the resulting json seems to mysteriously add a 1 to the end of the string. The output of my return string appears ...

Proper method for declaring a global variable within a React component

In the process of working on my react.js project, I have encountered a situation where I need all components to be able to access an object before the main component is rendered. My current approach involves passing the object as a prop to the main compo ...

Retrieve the modal ID when the anchor tag is clicked in order to open the modal using PHP

I am facing an issue with opening a modal and passing the id value using JavaScript. The id value is shown in a hidden input field. <a href="#modal2" data-toggle="modal" data-id="<?php echo $CRow['id'];?>" id="<?php echo $CRow[& ...

Retrieve the text value of a selected option in a v-select component

After reading some documentation about v-select and slots, I am a bit confused about whether it can be applied to my specific scenario on this codepen. All I really need is to capture the selected text (not the value) and utilize it somewhere in the code: ...

I recently created a "dist" folder through Babel. Is it possible to integrate the files within this folder directly into a fresh React project?

I have a React library that is available on npm. My process for publishing it involves the following steps: To begin, I create a folder called dist using Babel by executing this command- babel ./src -d ./dist Next, I upload the resulting dist directory ...

What is the process of manually loading webpack async chunks in the event that a dynamic import fails to load the file?

Async chunks in webpack can be created by using Dynamic Imports (for example: import('./ModuleA.js');). If the dynamic chunks fail to load, I want to retry loading them from another location. After grappling with the issue and delving into babel ...

Issue with inheritance from Angular ModalCtrl to ServiceCtrl not functioning as expected

I've been working on linking models to services in order to update global models throughout my app, but it doesn't seem to be functioning as expected. Recently, I delved into AngularJS and I suspect that I may have misunderstood my code. From wh ...

Can the value in a JavaScript object be updated dynamically when a button is clicked?

In my JavaScript code, there is an object named annualPlan. Whenever a user submits the HTML form for a specific month, I aim to update the value in the object for that particular month accordingly. For instance, if someone submits August 21 and 200, I w ...

Alter the colors of all Divs inside a container with each click

Hey everyone, I'm just starting to learn JavaScript. I'm working on a project where I want to change the color of all the divs in a container every time they are clicked. For example, on the first click, I want all the divs to turn red. Then, on ...

What could be the issue with this C# GridView code snippet?

I am facing an issue with a GridView that contains checkboxes. When I try to select a checkbox, I need to retrieve the values from that specific row. The problem lies in the fact that the chk variable in my C# code never evaluates to "true," preventing the ...

`When trying to utilize $uibModal.open within karma (AngularJS Factory), an error is encountered as it

My current task involves testing a function that triggers the opening of a $uibmodal. Below is my factory function: confirmationMessage: function (message) { var modalInstance = $uibModal.open({ templateUrl: 'views/templates/utilsTemplates/ ...

The IsKey property is not included in the return value of GetSchemaTable()

-Hello, everyone!- In the midst of developing a C# with ASP.NET project, I've encountered a challenge. The goal of the project is to dynamically load metadata and records from tables in order to edit them without specifying statically which tables ca ...

Retrieve the HTML code from a webpage on a different domain

Looking to extract HTML from another domain. Attempted solution: $.get("http://website1.com", function(data) { alert("Data Loaded: " + data); }); (not working) For example, as the owner of two websites: website1.com website2.com Now, the goal is to ret ...

Is there a way to collect multiple optional phone numbers dynamically using DOM manipulation?

I have a piece of code here where I am looking to dynamically add additional phone numbers as an optional field. Can anyone help me figure out how to achieve this? <div class="col-12 row border my-2 py-2"> <di ...