0

is there any other possibility to call a controller method in c# than using a form with an input element? Practically speaking I just want to execute a method by pushing a button or link.

so far I'm using:

@using (Html.BeginForm("RefreshQuestion", "Admin"))
{ 
    <input type="submit" value="refresh" class="questionInput" />      
}

many thanks in advance _tek

2 Answers 2

1

That is what you should do to call a controller method. If you don't want to use that code you can use an Action Link.

 @Html.ActionLink("Refresh", "RefreshQuestion", "Admin")

Do you need to post something as well or are you just making a call to the controller?

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a little javascript to make an ajax call. You can pass data from your UI to your action method.

The below example will post the value of Name input element to the action method when user clicks on the button.

<input type="text" id="Name" />
<input type="submit" id="btnSave" value="refresh" class="questionInput" />      

and your javascript is

$(function(){

  $("#btnSave").click(function(e){
     e.preventDefault();

     $.post("@Url.Action("RefreshQuestion","Admin")",
                                    { name : $("#Name").val() } ,function(data){
         // do something with the response
     });
  }); 

});

Your action method should have a parameter called name or a view model object which has a name property in it (model binding will do rest).

[HttpPost]
public ActionResult Add(string name)
{
  // save and return something
}

You may also use the serialize method as needed to send the data filled in all the items in a form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.