简体   繁体   中英

How to pass form text value as a c# parameter on Razor Pages

I have a function that fetch the data based on the user's registration number on the active directory and print it to the textbox fields on the modal. In that case I need to send as a parameter whatever is written in the textbox instead of the field numbered 31919 in the code below.

<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>

So, basically im trying to do something like this:

 @{
            FetchDataFromAD(RegNo.Text);
  }

PS: FetchDataFromAD just a function that modifying string expressions like that:

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");

        }

How can i achieve that? Thank you for any suggestions.

Consider to use the following:

<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>                       

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