Retrieve the output of a JavaScript function and submit it as extra form data

I am working on a JavaScript function that looks like this:

<script type="text/javascript">
    function doSomething() {
        var s = 'some data'
        return s; }
</script>

and

    @using (Html.BeginForm(new { data_to_send = x))
    {
       //some form controls that are sent to the controller via model
    }

My question is, is it possible and how can I assign the value returned by the doSomething function to the x variable in the form?

I don't need x in my model because it won't be stored in the database. It's just some additional information from the user on how to manipulate the data in the model before saving it to the database.

Edit: The controller action is:

public actionresult MyController(string data_to_Send, model) {}

Answer №1

Within the Visual:

@using (Html.BeginForm())
{
   @Html.HiddenFor(model => model.A1)
   @Html.HiddenFor(model => model.A2)
}

In the Model Structure:

public class MyModel
{
  public string A1 { get; set; }
  public string A2 { get; set; }
}

Inside the Controller Logic:

[HttpPost]
public ActionResult Details(MyModel model)
{
   string a1Value = model.A1;
   string a2Value = model.A2;
   return View(model);
}

Answer №2

In order to achieve the desired outcome, it is crucial to submit the form. You have the option to submit the model with PostedDateValue

 @using (Html.BeginForm("ActionMethod", "Controller", FormMethod.Post))
            {
                @Html.AntiForgeryToken()

                <div class="form-field required">
                    <label for="Posted Data">Posted Data</label>
                    <input  id="txtPostedData" name="PostedDateValue" >
                    <input type="submit"  class="gradient" value="SUBMIT" />
                </div>
            }
)

Controller

public ActionResult ActionMethod(string PostedDateValue)
        {
}

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

Utilizing React Refs to Retrieve Value from HTML Select Element

I am currently working on a form that includes the use of an HTML select element. <select className="form-control" id="ntype" required > <option value = "">None</option> <option value = "1">1</option> < ...

Positioning a Material UI Menu item underneath its parent element using CSS styling

I have created a Material UI dialog that features some text and an Icon with a dropdown menu option. You can check out the demo here: https://codesandbox.io/s/prod-rain-1rwhf?file=/src/App.js My goal is to properly position the Menu component so that it a ...

Anticipating the arrival of an external JavaScript file in the child component

I am facing an issue with my parent component that dynamically loads an external js file (similar to what is explained here). The child component needs a variable inside the js file, but it throws an error every time the page is loaded because the child c ...

Adding Bootstrap modal content to a webpage before it renders is a simple process that involves preloading the

When using Bootstrap modal, is it possible to load a remote URL along with the parent page without needing to click on a button? ...

At what point should the Version, Culture, and PublicKeyToken values for optional assembly be declared in the compilation/assemblies collection of the web.config?

I'm currently developing a managed HTTP Module for IIS that will be placed in the GAC. To finalize the installation process, I need to update the assembly information in the web.config file of a website. As per the guidance from MSDN documentation, th ...

Having trouble with the ::after element in my React component for my latest React 3D portfolio project

For my portfolio project, I am following a tutorial by LamaDev (https://www.youtube.com/watch?v=qALsVa-V9qo&t=2334s&ab_channel=LamaDev). At around timestamp 36:40, he creates a ::after selector which is not working for me, even though the rest of t ...

Adding a div tag within a while loop in ASP.NET using C#

I decided to name a webpage "profile.aspx" just like this. <div id="div1"> <asp:Label ID="Label1" runat="server" > //Name </asp:Label> <asp:Label ID="Label2" runat="server"> //Satutes </asp:Label ...

Which specific folder in Windows is accessible for reading and writing by everyone or the IIS User?

Recently, I have been working on developing a setup program using asp.net. I am looking to store some global information on the server machine. Registry seems like an option, but the default IIS User does not have write permission. I thought about using ...

Problem encountered with JavaScript getter on iOS 5

While implementing JavaScript getters in my iPad-optimized website, everything was working perfectly until I updated to iOS 5. Suddenly, the site stopped functioning properly. After thorough investigation, I discovered the root cause of the issue. I have ...

Is it possible for me to customize the default angular filter in order to prioritize displaying strings that begin with the search term?

In my current project, we are dealing with a massive list of strings (17,000+ at times) and have implemented an input box for filtering the list. Initially, I used Angular's default filter, but then a request came in to sort the list so that strings s ...

Adjust image size dynamically while keeping the original aspect ratio

Is there a way to scale variable portrait and landscape images dynamically to fit proportionally within a browser window? You can find my current image resizing attempt here: http://jsfiddle.net/6pnCH/4/ I would like the image to be already scaled vertic ...

Error message: WebTorrent encountered an issue and was unable to pipe to multiple destinations at once

Upon attempting to stream video from a provided torrent file, I encountered some unexpected issues. The example was taken from the official site, so one would naturally assume it should work seamlessly. To troubleshoot, I tried setting up a default site u ...

Issue with Checkbox Change Event in MVC Partial View

In one partial view page, I have three radio buttons - All Employees, Designation, and Confirmation Date. By default, the first is selected. When the third radio button is clicked, a date needs to be entered in a textbox. After that, an AJAX call is made a ...

Retrieve the ngModel's current value

One of the challenges I encountered is with a textarea and checkbox interaction. Specifically, once the checkbox is checked, I aim to have the value appear in the textarea. <div class="message-container"> <textarea *ngIf="mode === 1" ...

Error message: The Bootstrap .dropdown() method failed because it encountered an "Uncaught TypeError: undefined is not a function"

I've encountered an issue that seems to be a bit different from what others have experienced. Despite trying various solutions, I still can't seem to fix it. I suspect it might have something to do with how I'm importing my plugins. The erro ...

Run a chunk of HTML with dynamic PHP elements using JavaScript

Within a single .php file, there is a combination of HTML, PHP, and JavaScript. The JavaScript section is crucial for detecting the browser; if the browser is not Internet Explorer, JavaScript will trigger the HTML block containing PHP. Here is the JavaS ...

Using es6 map to deconstruct an array inside an object and returning it

I am looking to optimize my code by returning a deconstructed array that only contains individual elements instead of nested arrays. const data = [ { title: 'amsterdam', components: [ { id: 1, name: 'yanick&a ...

When using the Parse JS SDK with AngularJS UI routing, the login functionality may not properly retain the current user's

Below is the configuration and initialization of my module: angular.module('FXBApp', [ 'ui.bootstrap' ,'ui.router' ,'oc.lazyLoad' ,'parse-angular' ]). config(['$urlRouterProvider','$statePro ...

Adding information to an Excel spreadsheet using JavaScript

I'm facing a challenge in appending data to an existing Excel file using node.js. I've tried using the xlsx-writestream package with the code snippet below: var XLSXWriter = require('xlsx-writestream'); var writer = new XLSXWriter(&a ...

Disable Autocomplete Chip functionality when only one can be selected

When multiple is set to true, I prefer the Chip option. Is there a way to enable the Chip functionality even when multiple is set to false? <Autocomplete className={classes.search} options={top100Films} ge ...