views:

122

answers:

1

Greetings, i would like to prepare some good "machine" for calling asp.net mvc methods from jquery. The concept is as follows:

The concept is to have a one method each requests will be calling. By using visitor pattern I will be able to determine what kind of operation i should do.

public interface IRequestVisitor
{
    void VisitRequest_GetPersons(CRequest_GetPersons request);
    void VisitRequest_RemovePerson(CRequest_RemovePerson request);
}

Creating some abstract request:

public abstract class CRequest
{
    public abstract void AcceptVisitor(IRequestVisitor visitor);
}

and the implementation of concrete request:

public class CRequest_GetPersons: CRequest
{
    public CRequest_GetPersons(Guid schoolROWGUID)
    {
     SchoolROWGUID = schoolROWGUID;
    }
    public Guid SchoolROWGUID={get;private set;}

    public override void AcceptVisitor(IRequestVisitor visitor)
    {
     visitor.VisitRequest_GetPersons(this);
    }
}

And the implemetation of visitor:

public void CRequestVisitor : IRequestVisitor
{
    public CResponse ResponseResult {get;private set;}

    public void IRequestVisitor.VisitRequest_GetPersons(CRequest_GetPersons request)
    {
     //code responsible for getting persons
     Result = new CResponse_GetPersons_Success(List<Person>)
    }
    public void VisitRequest_RemovePerson(CRequest_RemovePerson request)
    {
     //code responsible for removing person
     Result = new CResponse_RemovePerson_Success();
    }
}

The action each js methods will execute is as follows:

public ActionResult AjaxRequest(CRequest request)
{
    CRequestVisitor visitor = new CRequestVisitor();
    request.AcceptVisitor(visitor);
    return JSON(visitor.Result);//return some result

}

I have also found how asp.net mvc methods can be called using jquery

<script type="text/javascript"> 
   $(document).ready(function() {         
     $("#GetPerson").click(function() {         
         $.getJSON("/Test/Request",
        function(data){
            //what should be here to display all persons ?
     //how can i pass guid of school?
          });
        });

        }); 
    });    

</script>

I would like to know how can I pass the rowguid of currently selected school. And how can I access list of persons returned as JSON object?

A: 

Hey,

You can pass parameters, that will be passed to parameters of the action method, by adding it to the URL:

/Test/Request?school=1

Replace 1 with the guid, and the guid will be passed back.

Brian
ok..and how to pass for instance whole object(e.g. of type Person)
niao
additionally i can't pass just guid. The expected argument is of type CRequest. Any ideas?
niao
You can't pass a whole object in a get operation; you would have to use an Html.BeginForm to post an object's data to the server.In either approach, you can't pass objects, you can only pass each property value of the object. So you could render all of the property values to HTML elements, and post them to the server, and reload the object (or requery the object if all you need is view only operation).So you can't pass a Person, but the person's name, address, and all other properties of person.
Brian