Home > Software design >  How to pass form text value as a c# parameter on Razor Pages
How to pass form text value as a c# parameter on Razor Pages

Time:02-07

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 ><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" >Fetch</button>
                        <span id="spin"><i style="position: relative; display: inline-block;" ></i></span>
                  </form>
      @{
            FetchDataFromAD("31919");
      }
            <hr />
            </div>
</center>

So, basically im trying to do something like this:

 @{
            FetchDataFromAD(RegNo.Text);
  }

P.S.: 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.

CodePudding user response:

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 ><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" >Fetch</button>
        <span id="spin"><i style="position: relative; display: inline-block;" ></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);
}
  •  Tags:  
  • Related