0

I have mvc application on a button click i'm changing hidden field value but i'm unable to use Request.Form["hiddenfield"] into private method ,

any way to use it?

Index.cshtml

<input type="hidden" name="hiddenfield" id="hiddenfield" value="true">

            <button type="button" id="stop" class="btn btn-success">

<script>
    if ($('#stop')) {
        $('#stop').click(function (e) {
            document.getElementById('hiddenfield').value = "false";
        });
    }
</script>

HomeController.cs

In private method i want to get this changed value do that i can do other stuff.

 private async Task Process()
        {
//unable to get hidden value using var test= Request.Form["hiddenfield"];

because its not controller method. but then how to check this changed hidden field value ?

As button click I can't call any mvc controller method because it is taking timme to hit method as another execution is already going on.

Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    Side note, `if ($('#stop')) {` will always be true. `$()` returns an object, and objects are truthy. If you are checking for existance you need to add `.length` to it, as 0 is falsy. But really you don't need that conditional. If the element is not found, the `click()` will simply be ignored. – Taplar Oct 30 '18 at 14:41
  • As for your question, have you inspected the submit request in your network console and verified that the hidden field is being sent on the request? – Taplar Oct 30 '18 at 14:43
  • Questions seeking debugging help must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Currently, you have not provided enough code to understand what you're doing, much less reproduce the problem. – Jack Oct 30 '18 at 14:47

3 Answers3

2

The MVC framework instantiates a controller per request and calls a method based on the routing configuration.

You can call a private method inside a public method and access the request via HttpContext.Current.Request.

There shouldn't be any issue with making a second request whilst the first request is still running, but they will be different instances of the same controller.

ste-fu
  • 6,879
  • 3
  • 27
  • 46
  • how can i check the hidden field value using HttpContext.Current.Request? – Neo Oct 30 '18 at 14:50
  • HttpContext.Current.Request.Form might work - lots of ifs and buts. Are you doing any of this asynchronously? – ste-fu Oct 30 '18 at 14:54
  • not working getting build error "object reference is required for non static field" , yes 1st while loop is working async. – Neo Oct 30 '18 at 14:58
  • HttpContext.Current is per thread, you will need to pass the Request part into the method as a parameter – ste-fu Oct 30 '18 at 15:36
  • ok thanks can you help me here as well https://stackoverflow.com/questions/53045087/unable-to-break-while-loop-immediately-on-button-click-c-sharp-mvc/53046066#53046066 – Neo Oct 30 '18 at 15:39
  • 2
    Sounds like your overall problem is rather complex for Q&A. You could try using a singleton class to manage state between requests maybe. Or maybe something like SignalR for two way communication – ste-fu Oct 30 '18 at 15:47
  • i'm able to resolved it most of it. if you could help me to stop the while loop on another button click , how to achieve it? – Neo Oct 30 '18 at 16:07
1

To see the value at the server I would use a Html helper

@Html.HiddenFor(model => model.SomeId, new { @id = "hfSomeId" })

or

@Html.Hidden("hfSomeId", someValue, new { @id = "hfSomeId" }
1

You cannot access a hidden field in a private method in MVC (as far as I know).

You will need to create a public action in the controller in order to post the updated value (the hidden field). Then from the controller action you can access the private method.

Something like:

public ActionResult Update(bool hiddenField)
{
    // do something with hiddenField and call private method Process()
}
haldo
  • 14,512
  • 5
  • 46
  • 52
  • yes but it is taking time to execute this method after button click :( because other process is going on while loop going on.. – Neo Oct 30 '18 at 14:49
  • 1
    I thought the question was how to access a hidden field in MVC private method. If it's taking time to execute then it sounds like you need to use asynchronous calls to the controller then update the UI once its completed. – haldo Oct 30 '18 at 14:58
  • thanks can me help me here as well - https://stackoverflow.com/questions/53045087/unable-to-break-while-loop-immediately-on-button-click-c-sharp-mvc/53046066#53046066 – Neo Oct 30 '18 at 14:59
  • how to call another action method immediately if one method processing is going on ? – Neo Oct 30 '18 at 15:05