Rockstar
Rockstar

Reputation: 59

Mvc controller method call from view button

I am trying to call Controller method from a Button in view.

Controller:

public ActionResult DownloadTemplate()
{       
    return View("DownloadTemplate");
}

[HttpPost]
public ActionResult onclick()
{
    Console.WriteLine("clicked");
    return View();
}

View:

<td class="rightalign">
   <span class="leftalign">                               
        @(Html.Kendo().Button()
          .Name("btnBlankTemplate")
          .HtmlAttributes(new { type = "button", @class = "k-primary" })
          .Content("Blank Template"))
   </span>

How can i Simply call the onclick() method by clicking the view button?

If Kendo MVC is not supporting how can I use simple Button?

Upvotes: 2

Views: 6218

Answers (3)

Rockstar
Rockstar

Reputation: 59

If you dont want to use Ajax and simply pass the value using Html.beginform().

controller:

public ActionResult DownloadTemplate()
{
  //To Do
}

View:
@using (Html.BeginForm("DownloadTemplate", "Controller", FormMethod.Post}))
    {
       <span class="leftalign">
       <input type="submit" id="btnBlankTemplate" class="k-button" value="Blank Template"/>
      </span>
    }

Upvotes: 0

Rockstar
Rockstar

Reputation: 59

I just added below and it is working.

Controller:

[HttpPost]
public ActionResult BlankTemplate()
{
    Console.WriteLine("clicked");
    return View();
}

view :

<script>
    BlankTemplate = function () {            
            $.ajax({
                url: _rootUrl + "Controller/BlankTemplate",
                data: {                   
                },
                type: "POST"
            });
        }
</script>

<span class="leftalign">
<input type="button" id="btnBlankTemplate" class="k-button" value="Blank Template" onclick="BlankTemplate();" />
</span>

Upvotes: 1

Ricky Gummadi
Ricky Gummadi

Reputation: 5240

The below should work.

                         @(Html.Kendo().Button()
                         .Name("btnBlankTemplate")
                         .HtmlAttributes(new { type = "button", @class = "k-primary" })
                         .Content("Blank Template"))
                         .onclick(ev => ev.Click("onClick")))

refer to the kendo ui documentation here

http://demos.telerik.com/aspnet-mvc/button/events

Upvotes: 1

Related Questions