猿问

将 Javascript Vairable 发送到 C# MVC 控制器

单击链接时,我需要将 JavaScript 变量发送到 MVC 中的某个 C# 控制器。我不想用 Jquery 来做。我需要用 JavaScript 来做。


这是控制器的代码:


 **[HttpPost]

        public ActionResult saveuser(int id, string propertyName, string value)

        {

            var status = false;

            var message = "";

            //Update data to database 


            using (PaediatricDBEntities db = new PaediatricDBEntities())

            {

                var user = db.ShiftTypes.Find(id);

                if (user != null)

                {

                    db.Entry(user).Property(propertyName).CurrentValue = value;

                    db.SaveChanges();

                    status = true;

                }

                else

                {

                    message = "Error!";

                }

            }

            var response = new { value = value, status = status, message = message };

            JObject o = JObject.FromObject(response);

            return Content(o.ToString());

        }**

我需要通过javascript向这个控制器发送3个参数,第一个是元素的id,第二个是元素的属性名,第三个是元素的值。谢谢,


慕虎7371278
浏览 104回答 1
1回答

12345678_0001

如果我理解您在这里尝试做的是使用纯 JavaScript 调用端点?这将为您完成工作:function callYourController(id, propertyName, value) {   var xhttp = new XMLHttpRequest();   xhttp.onreadystatechange = function() {       if (this.readyState == 4 && this.status == 200) {           console.log(this.responseText);       }   };   xhttp.open("POST", "www.google.com?id=" + id + "&propertyName=" + propertyName + "&value=" + value, true);   xhttp.send();}
随时随地看视频慕课网APP
我要回答