简体   繁体   中英

how to get value from document.getelementbyid in c sharp

I want to pass the element's value into a controller

JavaScript:

var element = document.getElementById("valueSelected");

Query String

Document.location="/ControllerName/ActionName/Value";

Ajax

$.Post('/ControllerName/ActionName/',{Parameter: element});

No problem

Script

function Delete() {
    $.getJSON("/ControllerName/ActionName/", {Id : Value}, function (data) 
       {
          alert(data)
       })
};

C#

public JsonResult ActionName(int? id)
    {
        string Data = "Sample Data";
        return Json(Data, JsonRequestBehavior.AllowGet);
    }

Let suppose Home is your Controller.

Add a function in your controller who can handle your ajax request. Suppose function name is getValue:

public void GetValue(string elementValue)
{
    //add your controller logic here
}

In your javascript, I use jquery to perform ajax request, you need to jquery to do this so:

var element = document.getElementById("valueSelected");
$.ajax({
     type: "POST",
     data : { elementValue : $(element).val() },
     url: '@Url.Action("GetValue", "Home")'
}).done(function (data) {
    console.log('done');
});

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