• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 65
  • Last Modified:

send data from client side to server in ASP.Net MVC

say, I have an IFrame integration with an another website. when my site opens in this IFrame, i can make few JS calls to get the data from another site.

once I have the data (string data), I populate a hidden field in my page with this data. Now, how do i post this back to my controller, or what are few ways to do this?

can it be a ajax call ? so far, what I have in my html page is, a script that runs as soon as the page loads

 
<script type="text/javascript">
    (function () {
        try{
                document.getElementById("data").value = GetData();
            }
        }
        catch (e) {
            alert("error");
        }
    })();
</script>

Open in new window

0
novice programmer
Asked:
novice programmer
  • 4
  • 3
1 Solution
 
zephyr_hex (Megan)DeveloperCommented:
AJAX or POST.  If you're using POST, you'll want your hidden fields to have name attributes, and to be placed inside a form.

If you're using AJAX, you can get the values of the hidden fields and send them with an AJAX call.

Either method requires some kind of event to trigger either the form submission or the "get the values" for AJAX.   You say that the hidden fields "get" the values.  Whatever is "getting" those values for the hidden fields could potentially be used to then trigger the POST or AJAX.  However, you may need to add logic which verifies that all of the hidden fields have the values before you proceed to sending them server side.

Can you expand on how the hidden fields get the values?
1
 
novice programmerAuthor Commented:
thanks @zephyr_hex.  here is my view code below. Instead of using a hidden field, I switched to a string field called "data", which is in my model.
so when this page loads in an IFrame of a another site, I can make a javascript call to get the data. you can see the line within the try catch block.
that's where I get the data.

say the data is a json or xml data. now how do i sent that back to my controller method?

-------------------------------------------------------------------------------------------------------------------------------
@model xyz.abcmodel
@{    
         ViewBag.Title = "demo";
    }
@Html.TextAreaFor(x => x.data, 20 , 50 , null)

@section scripts {
    <script type="text/javascript">
    (function () {        
        try {
                 document.getElementById("data").value = //call to get data
         }
        catch (e) {
            alert("error");
        }
    })();
    </script>
}
0
 
zephyr_hex (Megan)DeveloperCommented:
OK, well, since you already have the value in JavaScript, you might as well just make an AJAX call and send that data to your controller.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
novice programmerAuthor Commented:
@zephyr_hex - thanks.

I am new to this ASP.NET MVC. that's why I'm not sure how to do this.
the data i get, in this case is a XML file. How do i post that back to server. can you give me some tips on how i set the for the ajax call and
how can i set the controller method to get that data?
0
 
zephyr_hex (Megan)DeveloperCommented:
you can assign the return of the data to a variable, like so:

var dataValue = document.getElementById("data").value //after you've assigned the value of your data to this element. 

Open in new window


What I'm not clear on is what the structure of the data is.  You can use console.log(dataValue) to inspect the structure.  We need to know the structure of it in order to know if it needs to be restructured before sending via AJAX
0
 
novice programmerAuthor Commented:
@zephyr_hex  - the date I get is XML data.
0
 
zephyr_hex (Megan)DeveloperCommented:
I'm assuming by "XML data" you mean a string of XML., which is just a string.

The general structure of an AJAX call:


	$.ajax({
		url: '@Url.Action("Method", "Controller")',
		type: "POST",
		data: { data: dataValue },
		success: function(data) {
			//success
		},
		error: function(response) {
			//error
		}
	});

Open in new window



And your method in the controller will look something like:
[HttpPost]
        public JsonResult Method(string data )
        {
            var cityList = Cities.GetListByStateCountry(stateid, countryid);
            //do something
           //var result = "yay"!
            return Json(result);
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now