0

Im working in a MVC .NET application. I assign a value to an element ID like this:

document.getElementById("latbox").value = event.latLng.lat();

and I retrieve it to show it like this:

<p>Latitud: <input size="20" type="text" id="latbox" name="lat" ></p>

The thing is, how can I find this value in my controller? Thanks.

4
  • What are you trying to do exactly? Do you want to send the value of the 'latbox' input box to your controller? Or are you trying to set this value from your controller? You need to provide a more detailed use case Commented Jul 30, 2015 at 18:51
  • Sorry if I have explained wrong, I want to GET this value from my controller. Thanks. Commented Jul 30, 2015 at 18:53
  • You mean, you want to post this value to your controller? Commented Jul 30, 2015 at 18:57
  • Whatever I need to use the value in the controller, Thanks. Commented Jul 30, 2015 at 18:58

3 Answers 3

3

In your view, you can use a <input type='hidden' name='lat'/> that you assign the value you need. Note that it must match the name of your controller parameter

    @using (Html.BeginForm("Action", "Controller", FormMethod.Post))
    {
        <p>Latitud: <input size="20" type="text" id="latbox" name="lat"></p>
        <input type="hidden" name="lat" id="latbox-hidden" />

        <input type="submit" value="submit" />
    }

    <script type="text/javascript">
      var lat = document.getElementById("latbox");
      lat.value = "foo";

      document.getElementById("latbox-hidden").value = lat.value;
      alert(document.getElementById("latbox-hidden").value);
    </script>

And your controller:

    [HttpPost]
    public ActionResult Action(string id, string lat) //same name to bind
    {
             //do your thing
    }
Sign up to request clarification or add additional context in comments.

Comments

1

HTML fields are posted using the name attribute, which in your case is "lat". On the controller you could have a parameter like this:

string lat

It would get the value of the HTML input element.

1 Comment

Sorry, I get it now, this is a return value of my ActionResult. THANKS, It got clarified with the other answer
1

You can also use ajax call to send data to action

<head runat="server">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var serviceURL = '/AjaxTest/FirstAjax'; // give path of your action
        var param = document.getElementById("latbox");
        $.ajax({
            type: "POST",
            url: serviceURL,
            data: param,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: successFunc,
            error: errorFunc
        });

        function successFunc(data, status) {     
            alert(data);
        }

        function errorFunc() {
            alert('error');
        }
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.