Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  153] [ 7]  / answers: 1 / hits: 7528  / 5 Years ago, mon, september 9, 2019, 12:00:00

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 [email protected](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 }
);
}
}
}

More From » asp.net-mvc

 Answers
8

  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>

[#6308] Thursday, September 5, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;