How to call a method in Managed beans when I click in thymeleaf button - ejb

I want to know if it is possible to call a method present in my managed beans when I click in a button(the button is created with Thymeleaf).
Thanks

I do't know if there is an option to do it directly, I don't think so. But the solution could be easier: why not your button submits to a endpoint which executes this method?
<button onclick="execute()">Click me</button>
<script>
function execute() {
//$.get() or
$.post( "/the/endpoint", function(data) {
$('#test').html(data);
});
}
</script>
Then /the/endpointis your #Requestmethod which internally executes the method you want:
#Controller
public class YourController {
#GET
#Path("/the/endpoint")
public Response executeMethod() {
yourMethod(); //Executes your method
return Response.status(200);
}
}

Related

How to re-open modal window after ModalState is invalid with the errors shown?

I have a login modal window which has validation. The problem is that I can not re-open the modal window when the validation fails.
So, I have this controller: Index() which returns the View of the homepage. On that homepage, I have the modal window which has the validation.
But after clicking the submit button (Login() controller), and after validation failure, the URL is /Index/Login and I can not pass ViewBag value which tells the modal to re-open.
Here is the code from both controllers:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Login(Users user)
{
if (ModelState.IsValidField("username") && ModelState.IsValidField("password"))
{
if (Users.Login(user))
{
//some custom logic
return RedirectToAction("../Dashboard/Dashboard");
}
else
{
ViewBag.LoginError = "Wrong username/password";
}
}
else
{
}
return View("Index");
}
I really need help guys. I am trying to solve this the whole day!
PS: I work with Razor syntax on the front.
If you have the section scripts in your _Layout.cshtml which is in the /Views/Shared/_Layout.cshtml then paste the below code at the end of the /Views/Home/Index.cshtml.
#section scripts
{
<script>
#if(ViewBag.LoginError == "Wrong username/password")
{
WriteLiteral("$('#login-modal').modal('show');");
}
</script>
}
This can be done ajax way too however above is the solution to your question.
If the section scripts is not defined in _Layout.cshtml, then define this section at the end of the _Layout.cshtml just like below.
#RenderSection("scripts", required: false)

Asp.net mvc5 same action POST and GET in the same method

Can i send to the action method new record and then get the new id by the same method
public class HomeController : Controller
{
[HttpGet]
[HttpPost]
public JsonResult _sendConfirmation'(string subject,string mail)
{
Some Code--Some Code---Some Code
return Json(new { Success = true, id = newCreatedMailId });
}
}
Getting the id by jquery
$.getJSON('/Mails/_sendConfirmation', function (comingData) {
alert("success" + data);
jQuery.get('/Mails/_getNewMailSendConfirmation', { id: comingData }, function (data) {
jQuery('#myModal').modal('show');
jQuery('#myModal .modal-body').html(data);
});
enter code here
So your Ajax request is looking for an action named _sendConfirmation on your Mails controller and not finding it but you've chose. To just show us your home controller index action so we have no idea if the appropriate controller exists or not with that.
As far as having it decorated with a get and a post, when you do this you are essentially telling the action to look for data on the request in two different places which will most likely not end well for you unless you plan on doing that switch logic yourself inside the action. Your code would probably be a bit less error prone if you seperated the action and once extracted the data, you call the same helper methods to work your data.

Use the data from the event listener withing the function of the Event Dispatcher

Background:
I would like to use the data from the event listener from the functions that called the event dispatcher. From what I can see the event dispatcher does only returns the GenericEvent object.
The main bundle is where the dispatcher is being called.
MainBundleController.php
public function mainBundle() {
$this->getContainer()->get('event_dispatcher')->addListener('my_test_event', new GenericEvent($object))
// Is it possible to use the data from $anotherBundleListener
}
AnotherBundle/services.yml
another_bundle.listener:
class: ..../AnotherBundleEventService
tags:
- {name: kernel.event_listener, event: my_test_event, method: anotherBundleListener}
AnotherBundleEventService.php
public function anotherBundleListener(GenericEvent $event) {
//Do amazing things
return $somethingAmazing
}
The event listener and dispatcher works the only issue is I would like to use the data from the listener within the functions that called the dispatcher.
If there is another way of doing this please let me know.
I found the solution here http://symfony.com/doc/2.8/event_dispatcher/method_behavior.html
public function mainBundle() {
$myEvent = MyEvent();
$this->getContainer()->get('event_dispatcher')->addListener('my_test_event', $myEvent)
$data = $myEvent->getData;
}

Ajax with ViewComponent

How to use AJAX for ViewComponent in Asp.Net Core? That is calling the method #Component.Invoke ("ComponentName", SomeData); ViewComponent will involve various views depending on SomeData without rebooting the main view.
Update
My solution is:
$(function () {
$(Container).load('ControllerName/ControllerAction', {
ArgumentName: ArgumentValue });
});
Controller :
public IActionResult ControllerAction(string value)
{
return ViewComponent("ViewComponent", value);
}
Is there a way to directly use a ViewComponent as AjaxHelpers in previous versions?
Your solution is correct. From the docs:
[ViewComponents are] not reachable directly as an HTTP endpoint, they're invoked from your code (usually in a view). A view component never handles a request.
While view components don't define endpoints like controllers, you can easily implement a controller action that returns the content of a ViewComponentResult.
So as you suggested, a lightweight controller can act as an AJAX proxy to our ViewComponent.
public class MyViewComponentController : Controller
{
[HttpGet]
public IActionResult Get(int id)
{
return ViewComponent("MyViewComponent", new { id });
}
}

jQuery post to method in class

I am in a legacy asp.net application and I need to call a method within a class via jQuery ajax post.
So far I have a class called NewClass with a single method;
[WebMethod]
public string jQuery_GetCategoryDescription(string CategoryName)
{
return "Slappy";
}
I then have the following jQuery;
$.post('/NewClass/jQuery_GetCategoryDescription', { CategoryName: "trippy" }, function (newHTML) {
alert(newHTML);
});
I've tried putting in the whole namespace in.
However I can't seem to call the method within the class.
EDIT
I am getting a 405 error
If the
[WebMethod]
public string jQuery_GetCategoryDescription(string CategoryName)
{
return "Slappy";
}
Is in side Index.aspx
You can call it by /Index.aspx/jQuery_GetCategoryDescription

Resources