Call a method in a .cs from javascript

986 views Asked by At

I'm stacking with the following problem: I want to create a Template witch contains forms (3 textboxes and a button).Inside this template, through a javascript, have to call a function (CRUD method) that is inside a .cs.

So...that's one of my CRUD function in EmployeeBL.cs :

[WebMethod]

public static bool CreateEmployee(int Id, string Nome, string Cognome) 
{ ...} 

while here it's my Employee.tpl witch should call CreateEmployee():

<div class="container" style="max-width: 400px">

    <form class="form-horizontal" method="post" id="Form"
          data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
          data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
          data-bv-feedbackicons-validating="glyphicon glyphicon-refresh"
          data-bv-submitbuttons='button[type="submit"]'>
        <div class="form-group">
        {Message}
    </div>

    <div class="form-group">
        <input type="text" class="form-control" id="Id" placeholder="User name" value="{Model.Id}"
                data-bv-notempty-message ="{UserNameNotEmptyMessage}" />
    </div>
    <div class="form-group">
        <input type="text" class="form-control" id="FirstName" placeholder="First Name" value="{Model.FirstName}"
                data-bv-notempty-message="{FirstNameNotEmptyMessage}" />
    </div>
    <div class="form-group">
        <input type="text" id="LastName" placeholder="Last Name" value="{Model.LastName}" />
    </div>


    <div class="form-group">
        <button type="submit" class="btn btn-default" value="Submit" id="myButton" >Create Employee</button>
    </div>

Now always inside this tpl put a script like here:

<script type="text/javascript">
$(document).ready(function ()
{
$("#Form").bootstrapValidator();

    $("#myButton").click(function(){

  var Id=foo($('#Id').val());
  var FirstName= foo($('#FirstName').val());
  var LastName=foo($('#LastName').val());



 });

});

Summarizing: i need to create an Employee (with Id,LastName,FirstName) that goes to write on my DB via clicking on Button So my question is how to set visible the namespace of EmployeeBL.cs and how to call it's method CreateEmployee() inside script (clicking on Button)? Thx in advance!!

2

There are 2 answers

1
Yuri On BEST ANSWER

this should work

function createUser()
{
    $.ajax({
        type: "POST",
        url: "YourPageName.aspx/CreateEmployee",
        data: {Id: foo($('#Id').val()), Nome: foo($('#FirstName').val()),Cognome:foo($('#LastName').val()) },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
             alert('Ok');            }
        error: function (request, status, thrownError) {
           alert(thrownError);
   }
    });
}
1
Koalito On

@Yuri: First of all Ty so much for help.Same time i found out that is better avoid this way. It was my fault cause i skipped telling you i'm working with ACSPNET with Owin and MVC pattern. So i created MVC files and a Business Logic one..In Controller under the function Invoke() i have to find a method to call EmployeeBL.CreateEmployee() and same time return a template like here:

public class EmployeeController : Controller<EmployeeModel>
{
    public override ControllerResponse Invoke()
    {
        var claims = new List<Claim>{

           new Claim(ClaimTypes.Name,Model.Id.ToString()),
           new Claim(ClaimTypes.Name,Model.Name),
           new Claim(ClaimTypes.Name,Model.Surname)

       };

        return newTpl(GetView<EmployeeView().Get(Model,Html.MessageBox.Show(StringTable.EmployeeModel)) , StringTable.PageTitleCreateEmployee);
    }
}

where Get() function is defined in this way:

public class EmployeeView : View 
{
    public ITemplate Get(EmployeeModel viewModel = null, string message = null)
    {
        return
            TemplateFactory.Load("Employee.tpl")
                            .Model(viewModel)
                            .Set().Set("Message", message);
    }
}

Thus in Employee.tpl is defined only forms and button.