Running a Server Side Method/Process OnClick

2019-08-22 07:24发布

问题:

I am new to MVC. I have defined a button in one of my views (bootstrap):

<button type="button" class="btn btn-success btn-lg">
    <span class="glyphicon glyphicon-star"></span> Star
</button>

Now, I want to execute some server side code when this button is clicked however I am not sure how to do it in an MVC project. Where should place the server side code and how should I call it ? I am using Razor.

回答1:

your best bet is to look at jquery/ajax in the 1st instance as there are many. many examples on SO that allude to this pattern. Below is a quick sketch to get you going.

$(function() {
    $('.btn-success').on('click', function() {
        $.ajax({
            url: "your url in ironpython",
            data: {datavariable:somedatatosend},
            context: document.body
        }).done(function() {
            // update any divs required here with the
            // returned json result (or text)
            $(this).addClass("done");
        });
    };
};

if your target url was mvc, then you'd replace the url parameter above with something like:

url: '@Url.Action("myAction","myController")'


回答2:

MVC stands for model-view-controller. We use Controller Classes to handle incoming browser requests. Here you can call a method in your controller using java-script.

It is better if you can follow the MVCMovie tutorial to get a clear understanding.

Thanks!