控制器参数MVC ASP.NET

时间:2015-07-07 00:00:52

标签: c# json asp.net-mvc

我对MVC框架很陌生。 目前我正在从谷歌构建该请求REST API的应用程序并返回JSON对象(非常简单)。 我想从视图页面向控制器传递两个参数。将html表单的两个参数传递给控制器​​的正确方法是什么?

我的目标是发送REST请求,然后从JSON对象获取JSON对象和打印属性。 任何帮助都会非常感激。

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; }
        }
    }
}

Home / View Index.htmlcs页面

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

2 个答案:

答案 0 :(得分:0)

最简单的方法是将参数接受为decimal。像这样:

[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();
    }

}

然后,您可以在操作方法中将小数值转换为LatitudeLongitude值。

答案 1 :(得分:0)

您只需将结果返回为json并返回类型JsonResult。

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