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

mikha eAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
mikha eAuthor 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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mikha eAuthor 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
mikha eAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.