开发者

ASP.NET MVC JQuery - flexible alghoritm for calling asp.net mvc methods

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜