Incorporating TextBox Values into a List Using Bootstrap

Currently, I am in the process of developing an application using C# ASP.NET MVC 5 with Bootstrap and jQuery. My familiarity with JavaScript is limited at this time.

1. Initially, my question was how to modify the JavaScript to restrict input to a specific number of coordinates ('Quantity'). However, I have since found a solution and updated the code accordingly.

  1. I am now wondering how I can send the 'list' data to the Controller when the user clicks 'Submit'. I assume that the corresponding Model will need to include a List of x & y values to which I would bind the list?

Thank you!

Controller

[HttpGet]
public ActionResult Index() {

 var plate = new Plate() {

   Holes = new Holes() {
    Coordinates = new List<Tuple<double, double>>()
   },

 };

 return View(plate);
}

[HttpPost]
public ActionResult Index(Plate plate) {
 try {
  // access plate.Holes.Coordinates

  DrawingGenerator drawingGenerator = new DrawingGenerator(plate);
  string outputFileName = drawingGenerator.GenerateDrawing();

  ViewBag.fileName = outputFileName;

  return View("../Download/Success");
 } catch (Exception) {
  return View("ErrorPage");
  throw;
 }
}

cshtml file

<div class="tab-pane fade" id="tab2">
   <div class="form-inline col-md-5">
      @Html.TextBox("Quantity", null, new { @class = "form-control" })
      @Html.TextBox("X", null, new { @class = "form-control" })
      @Html.TextBox("Y", null, new { @class = "form-control" })
      <input type="button" id="Add" class="button1" value="Add" />
   </div>
   <ul id="list"></ul>
</div>

<script type="text/javascript">
$(document).ready(function () {

    // list container
    var listContainer = $('#list');
    var count = 0;

    $("#Add").click(function () {

        // get x  & y from tb
        var qty = $("#Quantity").val();

        if (count == qty)
        {
            alert("can't add more! update quantity");
            return;
        }

        var x = $("#X").val();
        var y = $("#Y").val();

        // add new list item
        listContainer.prepend('<li> (' + x + ', ' + y + ') </li>');
        count = count + 1;

        // clear value input
        $('#X').val('');
        $('#Y').val('');

    });
});

Answer №1

When a button is clicked, capture the content and send it to the server.

Check out this Fiddle for reference

$('#get').click(function(){
   alert( $('#list').text());
});

Use AJAX to post the content to the server

$('#get').click(function(){
  console.log( $('#list').text());
 $.ajax({  
        type: "POST",  
        url: "/path to controller file",  
        data: $('#list').text(),  
        success: function(data) {  
            console.log(data); //Post successful
        }  
    });  
});

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

Avoiding errors caused by higher order array methods

Is there a way to avoid errors when filtering data? The function below may encounter issues at conversationMember.Name.toLowerCase() if conversationMember is not present. This function is part of a computed property in a Vue application. Feel free to ask ...

Guide: "Executing an AJAX button click using a Greasemonkey script"

(find below a solution) I created a Greasemonkey script that automatically clicks on a specific link within a target website. Initially, my script looked like this: var MyVar = $("a:contains('Save')"); if (MyVar && MyVar.length) win ...

Problems with CSS: Struggles with Overflow-x and display:flex

I have already completed 2 questions regarding the current issue I am facing. However, I have now created a more improved example. Please take a look below. The boxes are being loaded using Angular ng-bind. Check out the code pen here: http://codepen.io/a ...

What is the best way to align sections side by side using Bootstrap?

Hello, I'm currently trying to customize the layout of this Bootstrap webpage to resemble the design shown in this image https://i.sstatic.net/w7i6Z.png. Specifically, I want to display the blog section and the team section side by side. As a beginne ...

Tips for smoothly applying a class to an image for seamless transitions, avoiding any awkward clunkiness

Check out my work on jsfiddle I understand that the image may not be visible, but I'll do my best to describe it. The header smoothly animates as I scroll down, but the image abruptly changes size instead of smoothly resizing. I want it to gradually ...

How about a unique scrolling experience?

Currently, I am attempting to prevent the scroll position from being locked after a single scroll for one second while scrolling down one section at a time. However, I am encountering unexpected behavior. const [nextSection, setNextSection] = useState(&ap ...

The order of the LinkedHashMap returned from the server is not maintained in the AJAX response

I have implemented a LinkedHashMap to store map entries sorted by a specific business rule, and then sending it to the client. However, I am facing an issue where my AJAX response is always sorted by the map key instead of the business rule. Here is the c ...

Can you provide some insight into why the init() method is throwing an error?

My project utilizes DynamoDB access through https://www.npmjs.com/package/react-native-dynamodb. I followed the code provided on the website for implementation. The issue I'm facing is that when hovering over my .init() method in WebStorm IDE, it sho ...

Generating Over 50,000 Text Particles with the Power of THREE.js

Seeking to generate a character particle system with over 50,000 individual letters. Came across a similar solution using XG here. The main challenge lies in the application's performance when creating this. Here is some concise pseudo code: var f ...

Is it possible to verify the data within a VueJS model? It seems that none of the Vue validators are effective

Hello there, It's common knowledge that using Vue-validator with most UI components can be challenging when it comes to validation. I've been utilizing Vue Material Components by @mjanys, which is a fantastic library. The author has included met ...

Update the table with information from a designated URL every 5 seconds

So here's the situation: I've got a dynamic HTML table <table id="example"></table>. The data displayed in this table changes according to the URL. Normally, my default URL looks like this: index.php?action=add To handle this, I&a ...

Combining multiple JSON strings into a single object using JavaScript

I am facing an issue with parsing a JSON output that contains two strings with specific formats: [{"device_id":"9700015","update_time":"2017-01-04 18:30:00","sensor_value":"1287.6"}] [{"device_id":"9700016","update_time":"2016-12-31 18:30:00","senso ...

Is there a way to configure cloudantDb.search to fetch all records without the default limitations of 25 records and

I've been exploring ways to optimize search functionality by utilizing search indexes. Here is the code snippet I've been working with: return new Promise((resolve, reject) => { let conf = {'include_docs': true} conf.q = "user: ...

What is the correct way to showcase a list of errors below my component?

In my React 16.13.0 and Bootstrap 4 project, I'm looking to show a list of field errors based on a specific variable in my component. The current code I have is as follows: {props.errors && props.errors[props.name] && ( <Form ...

AngularJS - A Guide to Determining the Time Span Between Two Dates

I am trying to figure out the exact number of months and days between two specific dates. For example, if the start date is "Jan 12, 2014" and the end date is "Mar 27, 2017", the result should be "38 months and 15 days". However, I am currently only able ...

Using Twitter Bootstrap for styling, implement a jQuery AJAX post request

After testing the first "alert" line, it seems that the function is working correctly. However, the second alert line (testing) does not seem to be functioning as intended. function updateCurrencyBudget(code, exchangeRate, http){ alert(http + '/a ...

Error in MEAN CRUD operation cannot be determined

{ text: undefined, done: false, _id: 529e16025f5222dc36000002, __v: 0 } PUT /api/todos/529e16025f5222dc36000002 200 142ms - 68b Encountering an issue while updating my CRUD todo list. Despite receiving a successful status code of 200 after submittin ...

Identify alterations in Ember's textarea

Recently, I have been working on creating a new user interface that includes an Ember textarea: {{textarea value=name cols="80" rows="6"}} However, I am unsure about how to detect when a user makes changes (perhaps by using an action) so that I can promp ...

Remove the URL parameters from $_GET and then show a notification message after the update is complete

To achieve the task of changing the user status (active/inactive) from a button, removing all GET parameters from the URL, and showing a bootstrap warning message. Current URL: http://localhost:8282/userslist.php?deactive=59 Desired clear URL: http://loc ...

What's causing these divs to be misaligned in various directions?

I am currently working with a front-end framework based on flexbox called Material-UI, and I have noticed that the layout of certain components, particularly the quantity control elements, appears to be different even though they all share the same styling ...