简体   繁体   中英

Controller Parameters MVC ASP.NET

I am pretty new to the MVC framework. Currently i am building an application for that request REST API from google and return the JSON object(pretty simple). I would like to pass two parameters to the controller from the view page. What would be the right way to pass the html form's two parameters to the controller?

My goal is to send a REST request then get the JSON object and print properties from the JSON object. Any help would be much appreciate.

The controller in controllers/homeController.cs

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string MyString = ViewBag.Message =  "REST API Application";

            return View();
        }

        [HttpPost]
        public ActionResult SendRestRequest(Latitude latitude, Longitude longitude)
        {

                var stopwatch = new System.Diagnostics.Stopwatch();
                var timestamp = new System.Runtime.Extensions(); 


                    // fetch data (as JSON string)

                    var url = new Uri("https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&key=ggkiki9009FF");
                    var client = new System.Net.WebClient();
                    var json = client.DownloadString(url);

                    // deserialize JSON into objects
                    var serializer = new JavaScriptSerializer();
                    var data = serializer.Deserialize<JSONOBJECT.Data>(json);

                    // use the objects

                    decimal DstOffset = data.dstOffset;
                    decimal RawOffset = data.rawOffset;

                    ViewBag.jsonDstOffset = data.dstOffset;
                    ViewBag.jsonRawOffset = data.rawOffset;
                    ViewBag.jsonStatus = data.status;
                    ViewBag.jsonTimeZoneId = data.timeZoneId;
                    ViewBag.jsonTimeZoneName = data.timeZoneName;


            return View();
        }

    }

    namespace JSONOBJECT
    {
        public class Data
        {
            public decimal dstOffset { get; set; }
            public decimal rawOffset { get; set; }
            public string status { get; set; }
            public string timeZoneId { get; set; }
            public string timeZoneName { get; set; }
        }
    }
}

The Home/View Index.htmlcs page

<form action="@Url.Action("SendRestRequest", "Home")" method="post" target="_blank">
        Latitude:<br>
        <input type="text" name="Latitude" value=""><br>
        Longitude:<br><br>
        <input type="text" name="Longitude" value=""><br><br>
        <input type="submit" value="Send REST Requst">
    </form>

The easiest way is to accept the parameters as decimal . Like so:

[HttpPost]
public ActionResult SendRestRequest(decimal latitude, decimal longitude)
    {

            var stopwatch = new System.Diagnostics.Stopwatch();
            var timestamp = new System.Runtime.Extensions(); 


                // fetch data (as JSON string)

                var url = new Uri("https://maps.googleapis.com/maps/api/timezone/json?location=" + latitude + "," + longitude + "&timestamp=1331161200&key=ggkiki9009FF");
                var client = new System.Net.WebClient();
                var json = client.DownloadString(url);

                // deserialize JSON into objects
                var serializer = new JavaScriptSerializer();
                var data = serializer.Deserialize<JSONOBJECT.Data>(json);

                // use the objects

                decimal DstOffset = data.dstOffset;
                decimal RawOffset = data.rawOffset;

                ViewBag.jsonDstOffset = data.dstOffset;
                ViewBag.jsonRawOffset = data.rawOffset;
                ViewBag.jsonStatus = data.status;
                ViewBag.jsonTimeZoneId = data.timeZoneId;
                ViewBag.jsonTimeZoneName = data.timeZoneName;


        return View();
    }

}

You can then convert the decimal values to Latitude and Longitude values in your action method.

You just return result as json and return type JsonResult.

[HttpPost]
public JsonResult SendRestRequest(decimal latitude, decimal longitude)
{
    return Json(your content);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM