如何在Razor Pages上将表单文本值作为c#参数传递



我有一个函数,可以根据用户在active directory上的注册号获取数据,并将其打印到模态上的文本框字段中。在这种情况下,我需要将文本框中写的任何内容作为参数发送,而不是下面代码中编号为31919的字段。

<center>
<image style="margin-top: 20px; width: 25%;" src="/images/adduser.png"></image>
<div style="margin-top: 25px;">
<form method="post">
<label class="control-label"><b>Reg. No</b></label> 
<input type="text" name="RegNo" id="RegNo" value="">
<button onclick="created(1)" style="margin-left: 15px; height: 60%;" data-toggle="modal" data-target="#AddUser1" type="button" class="btn btn-light-primary px-6 font-weight-bold">Fetch</button>
<span id="spin"><i style="position: relative; display: inline-block;" class="fas fa-sync fa-spin"></i></span>
</form>
@{
FetchDataFromAD("31919");
}
<hr />
</div>
</center>

所以,基本上我想做这样的事情:

@{
FetchDataFromAD(RegNo.Text);
}

附言:FetchDataFromAD只是一个修改字符串表达式的函数:

public void FetchDataFromAD(string RegNo)
{    
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(directoryEntry);
search.Filter = "(postalCode=" + RegNo + ")";
foreach (System.DirectoryServices.SearchResult sResultSet in search.FindAll())
{
// Ad
FetchADPersonelName = GetProperty(sResultSet, "cn");
FetchADTitle = GetProperty(sResultSet, "title");
FetchADNetworkN = GetProperty(sResultSet, "samaccountname");
FetchADEmail = GetProperty(sResultSet, "mail");
FetchADAdress = "2";
// FetchADDepartman = GetProperty(sResultSet, "department");
}
}

我怎样才能做到这一点?谢谢你的建议。

考虑使用以下内容:

<script>
function ApplyFilter() {
var regno = document.getElementById("RegNo").value;
$.ajax({
type: "GET",
url: '@Url.Action("FetchDataFromAD", "Home")',
contentType: "application/json;charset=utf-8",
data: { regno },
dataType: "json",
success: function (data) {
}
}); 
};  
</script>
<div style="margin-top: 25px;">       
<form method="post">
<label class="control-label"><b>Reg. No</b></label>
<input type="text" name="RegNo" id="RegNo" value="">
<button onclick="ApplyFilter()" style="margin-left: 15px; height: 60%;" data-toggle="modal" data-target="#AddUser1" type="button" class="btn btn-light-primary px-6 font-weight-bold">Fetch</button>
<span id="spin"><i style="position: relative; display: inline-block;" class="fas fa-sync fa-spin"></i></span>
</form>       
<hr />
</div>                       

以及从JavaScript:调用的FetchDataFromAD()方法签名更新

public JsonResult FetchDataFromAD(string RegNo)
{
... your code here   
return Json("Filter is applied", JsonRequestBehavior.AllowGet);
}

相关内容

  • 没有找到相关文章

最新更新