Call Action Method on button click ASP.NET MVC

user11431955

I am trying to get user input in button click. When user insert number and press Check, it needs to return xml data type. So in my controller I create function which will return a data for passing ID

[ResponseType(typeof(AKONTA))]
        public IHttpActionResult GetAKONTA(string id)
        {
            AKONTA aKONTA = db.AKONTAS.Find(id);
            if (aKONTA == null)
            {
                return BadRequest("Ne postoji A_KONTO pod tim rednim brojem");
            }

            return Ok(aKONTA);
        }

And In my View I have following

<br /><br />
<form>
    <div class="form-group">
        <label>A_KONTO</label>
        <input type="text" class="form-control" aria-describedby="AKONTO BROJ" placeholder="Unesite broj AKONOTO">
    </div>

    <div class="form-group">
        <a asp-action="Index" class="btn btn-primary" id="aKonto" action="@Url.Action("GetAKONTA", "Akontas")">Provjeri</a>
    </div>
</form>

And I want to create in btn click when user pass ID it needs to return XML data format.

IMAGES

SO far I create a JS function, but I don't know JavaScript and don't know the logic how to pass Controller Action Result to JS.

  <script>
        $(document).ready(function () {
            $('#aKonto').click(function () {
                document.getElementById("aKonto").onclick = function () {GetAKONTA()};;
            });
        });
    </script>

If someone can help me I would be very thankful. Cheers !

UPDATE

function aKontoSubmit() {
            $.ajax({
                type: "GET",
                url: 'api/Akontas',
                //data: { id: id },
                dataType: "xml",
                success: function (result) {
                    // action to do after form submit
                },
                error: function () {
                    alert("Ne postoji AKONTO pod tim rednim brojem");
                }

            });
        }

**routeConfig**

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace AkontasWebApi
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
ArulPrasath A M
  1. Add Reference of Jquery, to try the ajax call method.

    function aKontoSubmit() {
    $.ajax({
        type: "POST",
        url: '/Akontas/GetAKONTA',
        data: $('form').serialize(),
        dataType: "json",
        success: function (result) {
            // action to do after form submit
        },
        error: function () {
            alert("Error while inserting data");
        }
    });
    

    }

    1. Change you Link Code as Below
 <a asp-action="Index" class="btn btn-primary" id="aKonto"  onClick='return aKontoSubmit() '>Provjeri</a>

Or Else You Can try if you are using ASP.Net MVC Core Development

<form asp-action="GetAKONTA" asp-controller="Akontas" method="post"> 
    <div class="form-group">
        <label>A_KONTO</label>
        <input type="text" class="form-control" aria-describedby="AKONTO BROJ" placeholder="Unesite broj AKONOTO">
    </div>

    <div class="form-group">        
         <input class="btn btn-primary" id="aKonto"  type = "submit" value = "Provjeri" /> 
    </div>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

AJAX call in ASP.NET MVC application not calling Action method

Call an action method from layout in ASP.NET MVC

ASP.NET MVC Knockout button click call POST API

Click a button, call a method in controller in ASP.NET

Call a method from button click in ASP.net, using controller

Call mvc action with parameters on button click

ASP.Net MVC keeps calling the index action method for each controller action method I call

In ASP.NET MVC: All possible ways to call Controller Action Method from a Razor View

How to call an action method upon selection from the <select> dropdwon in ASP.NET Core 6 MVC?

jQuery to call Action Method in ASP.NET MVC C# by Ajax

How to call controller from the button click in asp.net MVC 4

How to call asp.net mvc form submit event which use ajax after button click and validate?

Call a server side MVC action on the click of a Kendo UI button

How to get a HTML submit button in an ASP.NET page to call a controller method in MVC controller class

Can an HTML button call an MVC Controller Action method

Post back Method not get call on button click in MVC4

Call Controller Action on button click

How to prevent of twice call one action in asp.net mvc?

ASP.NET MVC initialize Controller and call action programatically

How to call controller action with a routeValue parameter in ASP.NET MVC

ASP.NET MVC two calls by button to same view action

Tow way for call a action in asp.net mvc with route attribute - Routing In asp.net mvc

ActionName Attribute & UrlHelper.Action Method in ASP.NET MVC

Pass Comma Separated Value to ASP.NET MVC Action Method

asp.net mvc 5 asynchronous action method

Calling a Method After a Controller Action in ASP.NET MVC

How to pass an object to an action method in ASP.NET Core MVC

ASP.NET MVC Return Model From View To Action Method

C# ASP.NET MVC Create Button is not working on click