Tips for displaying a dynamic ng-model list in ASP.NET MVC

Code Snippet

public class DynamicModel
{
   public List<string> PayoutID { get; set; }
   public List<string> PayoutType { get; set; }
}

Controller Implementation

[HttpPost]
public ActionResult DynamicDiv(DynamicModel model)
{
  return View();
}

View Using AngularJs Model Binding

<script src="~/Scripts/angular.js"></script>
    <script>
        var app = angular.module('myApp',[]);
        app.controller("myController", function ($scope, $http) {
            $scope.Payout = {
                PayoutID: '',
                PayoutType: ''
            };
            $scope.SendDynamicData = function () {
                $http({
                    method: 'post',
                    url: '/Home/DynamicDiv',
                    data: { model: $scope.Payout }
                }).success(function (data) {
                    alert(data.Message);
                }).error(function () {
                    alert("Something went Wrong");
                });
            };
        });
    </script>

<div ng-app="myApp" ng-controller="myController">
@using (@Html.BeginForm("DynamicDiv", "Home", FormMethod.Post))
{

    <div>
        <div class="form-group">
            <label>Enter Payout ID</label>
            <input type="text" class="form-control" ng-model="Payout.PayoutID" name="Payout.PayoutID" />
        </div>
       <div class="form-group">
            <label>Enter Payout Type</label>
            <input type="text" class="form-control" ng-model="Payout.PayoutType" name="Payout.PayoutType" />
        </div>

        <div class="form-group">
            <input type="button" class="btn btn-success" ng-click="SendDynamicData()" value="Submit" />
        </div>
    </div>
}
</div>

If multiple text-boxes are added dynamically in the front end, the values should be in the form of list<string>. However, one text-box works fine with a single ng-model.

How can these dynamic input values be sent to the controller?

Answer №1

In my opinion, it would be more practical to simplify your model by having a DynamicObject that consists of just one node with an ID and Type. By creating a list of DynamicObjects, you can easily share this structure throughout your application.

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

UI-Router is malfunctioning, causing ui-sref to fail in generating the URL

I'm currently working on a project that involves Angular, Express, and UI-router for managing routes. While I've properly configured my $states and included the necessary Angular and UI-router libraries in my HTML file, I am facing an issue wher ...

Having trouble executing multiple file uploads with node.js resulting in an ERR_EMPTY_RESPONSE?

Currently, I am attempting to upload multiple files on FTP using node.js. Everything seems to be going smoothly as the files are successfully uploaded to the server location. However, after some time passes, I do not receive a success message. Instead, I e ...

Angular allows for the creation of nested dynamic variables

I have a scenario where I need to use one dynamic variable within another dynamic variable in Angular. To illustrate, here is a snippet of what I am aiming for: The desired outcome of this code snippet is "John Doe". My question is, how can I achieve usin ...

Utilizing a foreach loop to control the behavior of two distinct radio buttons as a unified

Can someone help me with a code containing a 5 star rating system (radio button) for two different questions? The issue is that the ratings for each question are linked, so when I make a selection in one question, it clears the selection in the other. It& ...

Validating an Angular form upon submission using a function name with dots

Does anyone know how to call a 'custom validation directive' by watching the ngModel? In my submit function, the name is dotted.[sllr.save()] <div class="col-sm-10"><input type="text" placeholder="must be numeric" class="form-control" ...

Error code ENOSELF was encountered while trying to install angular-notification-icons using npm

Currently, I am utilizing the following link as a guide to construct a Notification system: https://github.com/jacob-meacham/angular-notification-icons The initial step involves executing: npm install angular-notification-icons --save I'm uncerta ...

Generating documents in Word or PDF format using PHP and Angular data

My goal is to generate a Word document using PHP for which I found a solution involving the use of headers. header("Content-type: application/vnd.ms-word"); header("Content-Disposition: attachment;Filename=output.doc"); Initially, this method worked well ...

Obtain the node identifier within the Angular UI Tree component [angular-ui-tree]

Utilizing Angular UI tree to create a relationship between items and categories has been successful in the default setup. However, a new requirement has emerged to incorporate node/section numbering within the tree for managing hierarchy. I attempted to i ...

Guide on parsing and totaling a string of numbers separated by commas

I am facing an issue with reading data from a JSON file. Here is the code snippet from my controller: myApp.controller("abcdctrl", ['$scope', 'orderByFilter', '$http', function ($scope, orderBy, $http) { console.log('abc ...

Saving a file with its original filename using ng file upload on the server: Tips and tricks

I am having an issue with ng file upload where my files are being saved on the server with a different file name. I want them to be saved with their original file name and correct extension (.jpg, .pdf). Here is my code snippet. Controller: $scope.uploadP ...

"Enhancing User Experience with AngularJS by Dynamically Modifying and Refresh

I'm currently attempting to dynamically add HTML elements using JavaScript with a directive: document.getElementsByClassName("day-grid")[0].innerHTML = "<div ng-uc-day-event></div>"; or var ele = document.createElement("div"); ele.setAttr ...

Alert notification service malfunctioning in the controller due to a typeError: The property 'alerts' is undefined and cannot be read

I encountered an issue with capturing the application response error. While trying to capture the error, I end up receiving the error itself. Within the Interceptor, based on the response code, I have set up a broadcast for $rootScope and displayed an ale ...

I am experiencing an issue with my code where it is failing to read the value from the

Just starting out with C-sharp and encountering some issues. I can't seem to get my code to read the value from app.config. file, and I'm not sure what's missing. Although the code opens Chrome browser, it fails to navigate to the Google web ...

How can I distinguish between a decimal and a long numeric token when using a custom JsonConverter with Utf8JsonReader?

I am working with a json converter that needs to convert a given property value to either a decimal or a long, depending on the type of value. However, I am facing an issue where I cannot determine if the property value is a decimal or a long since the t ...

Displaying a momentjs object within Angular templates

Looking to display a momentjs object, not an ISO8601 string, in AngularJS templates. For example: $scope.time = moment(); When I try the following in my template: <span ng-bind="time"></span> or <span>{{time}}</span> I want to ...

Parsing numerous JSON documents

Utilizing Newtonsoft.json, I attempted to deserialize a collection of JSON files. My C# classes align perfectly with the schema and work well. However, when I separate the JSON files into multiple readable files, even with links between them, Newtonsoft.js ...

What is the best method for effectively integrating a filter into an Angular application?

I'm encountering an issue with my Angular filters and could use some assistance. Would you mind checking out the following link: When I select 'Hitchens' from the dropdown menu, it displays information for both Hitchens and The Others. I am ...

Unable to Update the Status Code

When it comes to setting the status code to 9999, I am utilizing the basic Response.StatusCode. In this case, the page is accessed via an AJAX call and the status code is checked at readyState = 4. On detecting 9999 as the status code, an alert pops up wit ...

Utilize JSON parsing to display information in an Angular application

I am facing an issue with displaying JSON data in Angular. Although I have successfully sent the data from the backend to the frontend (Angular), I am unable to display it. To troubleshoot, I attempted to recreate a similar scenario on JSFiddle, even thou ...

What is the process for passing pre-determined functions as parameters of another function?

In my programming project, I have designed two classes. The first class, TextElement, is a selenium extension with various text properties where the Text method retrieves a string value from the element. The second class, ArrayTextElement, is essentially a ...