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

Issue

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 I’m trying to do something like this:

 @{
     FetchDataFromAD(RegNo.Text);
 }

P.S.: FetchDataFromAD just a function that modifying string expressions like that:

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

    }
}

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

Solution

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>                       

And the FetchDataFromAD() method signature update to be called from JavaScript:

public JsonResult FetchDataFromAD(string RegNo)
{
    ... your code here   

    return Json("Filter is applied", JsonRequestBehavior.AllowGet);
}

Answered By – Jackdaw

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *