I am completely new in MVC4 studying for a few weeks, I am asking how to call a Public Void
from View
to Controller
and what is the best procedure to call? using JavaScript? or Html.Action
?
Here is my Code in Action Controller:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public static string SearchAd(string LoginName = "")
{
string x = "Not Found";
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://lbcone");
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(SAMAccountName={0}))", LoginName);
var user = directorySearcher.FindOne();
if (user != null)
{
x = "Found";
}
return x;
}
Here is my Code in Javascript it has an error:
<script type='text/javascript'>
function SetValue(ctl, event) {
event.preventDefault();
var LoginName = document.getElementById('_LoginName').value;
var x = SearchAd(LoginName);
alert(x);
}
</script>
Here is my View button:
button type="button" onclick="SetValue();">
In this way will work fine :
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public static ActionResult SearchAd(string LoginName = "")
{
string x = "Not Found";
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://lbcone");
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(SAMAccountName={0}))", LoginName);
var user = directorySearcher.FindOne();
if (user != null)
{
x = "Found";
}
return Json(x, JsonRequestBehavior.DenyGet);
}
The JQuery script used to call this action method:
var LoginName = document.getElementById('_LoginName').value;
var requestData = {
LoginName :LoginName
};
$.ajax({
url: '/ControllerName/SearchAd',
type: 'POST',
data: JSON.stringify(requestData),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function (xhr) {
alert('Error: ' + xhr.statusText);
},
success: function (result) {
alert(result);
},
async: true,
processData: false
});
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.