为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕虎7371278 2022-10-23 16:27:17
单击链接时,我需要将 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,第二个是元素的属性名,第三个是元素的值。谢谢,
查看完整描述

1 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

如果我理解您在这里尝试做的是使用纯 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();

}


查看完整回答
反对 回复 2022-10-23
  • 1 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号