pagination not working with new php function via ajax - laravel-5.7

I am fetching records via ajax with search parameters and want to have pagination also. It works fine if I use it with the same function which loads view, for example
function viewOrder(Request $request){
$data = products::where('categoryId', 1)->paginate(1);
if ($request->ajax()){
return view('store.showAjax')->with('data',$data);
}
return view('store.show')->with('data',$data);
}
But my need is like this
function viewOrder(){
return view('store.show');
}
function searchOrder(Request $request){
$data = products::where('categoryId', 1)->paginate(1);
return view('store.show')->with('data',$data);
}
In this way It creates pagination but when I click on page link it load store.show view twice in the same html page. How can I achieve correct pagination this way?
Doing everything in viewOrder() function looks messy.

I just find out how to do it, simply passing full url instead passing ?page=1 in javascript function `
function getData(url, page){
$.ajax({
url: url, //'?page=1',
type: "get",
datatype: "html",
success: function(data){
$("#tag_container").empty().html(data);
location.hash = page;
}
});
}`

Related

Post Method leads to reload instead it is a void method

I have a Post Method which looks like
public void circleUpPost(HttpServletRequest _request, Model _model, #ModelAttribute("circleUpModel") CircleUpModel _circleUpModel) {
...
Within this POST Method I´m processing some stuff. When the Post method is finished the page reloads. But instead nothing should happen.
I already tried to add #ReponseBody or #ResponseStatus(value = HttpStatus.OK) but that doesn´t work!
Are there any suggestions?
EDIT
Maybe the submit button on my page causes the problem? But of course I have to submit my data from the view to the Post Method?
Okay after I found out it really was because the submit button leads to reloading the page I replaced the submit function.
Instead I used Ajax to send the data to my Post Method !
function sendData(){
var myform = document.getElementById("circle_form");
var form = new FormData(myform );
$.ajax({
url: "CircleUp",
data: form,
cache: false,
processData: false,
contentType: false,
type: 'POST'
});
}
Sorry for this "unnecssary" post! Hopefully it can hels someone else !

button in ASP.NET are all post method?

Are all button control has to be post method? or we can set it to get method, for example, I want to see an employee details by giving employeeId and click submit button
There is no difference between GET and POST method. They both provide url and parameters. POST method only have some advantages and some restrictions.
If your button is on form (as in classic asp.net), and there is no javascript handler for this button - only POST method can be here.
If you create jquery code (or pure javascript), that overrides default behaviour of the button, you can select what method use: POST or GET
<script>
$('#button').click(function() {
$.ajax({
url: '....',
data: { ....},
type: 'GET', //or 'POST'
success: function(res) {
//all fine
},
error: function() {
//invalid url or server error
}
};
return false; //to avoid default submit
});
</script>

How to manually render a template in Meteor?

The Discover Meteor book shows how to use the sasha:spin package to show a loading spinner template (<template name="loading">) while IronRouter waits for data.
How do I use this same loading template while I'm waiting for a regular jQuery ajax call to finish?
var locationInfoByZipcode = function(zipcode, callback){
$.ajax({
url: "http://api.zippopotam.us/us/" + zipcode,
type: "GET",
beforeSend: function(){
// Render the loading template. I tried Blaze.render("loading") but I'm not using it right
}.
success: function(response){
// Stop the loading template.
},
error: function(){
callback("error");
}
});
};
Blaze.render takes a template and a parent, not a string, so it'd be Template.loading and then the parent template you want to render into. You'd probably want to destroy it in the success callback.
What might be a little bit cleaner is putting the HTTP req inside a method along with a reactive variable & calling that method on click. Then, you can keep the loading template inside an #if reactiveVarIsTrue type thing in spacebars. Just a personal preference to not use jquery ajax calls if I can help it because they're not very expressive.
I got it.
var locationInfoByZipcode = function(zipcode, callback){
var renderedView = {};
$.ajax({
url: "http://api.zippopotam.us/us/" + zipcode,
type: "GET",
beforeSend: function(){
// $('body')[0] is the DOM node object that .render() needs
renderedView = Blaze.render( Template.loading, $('body')[0] );
},
success: function(response){
// to remove the template you need to pass Blaze.remove() the returned value from the initial Blaze.render() call
Blaze.remove(renderedView);
callback(response);
},
error: function(){
callback("error");
}
});
};

Issue loading Mvc 4 view in Iframe? Not displaying the model properties in IFrame

I have this following requirement. Need to display the html Body of the Mime message in IFrame
View
<iframe id="myIframe1" src="http://localhost:23245/Home/GetMimeMessageContent?FilePath=D \MimeFiles/htmlBody-small1.eml&PartName=HtmlBody" style="width:600px;height:600px;" >
Controller
public ActionResult GetMimeMessageContent(string filePath,string partName)
{
var mimeModel = BuildMimeModel(filePath, partName);
MimeHeaderModel mimeHeadermodel = new MimeHeaderModel();
mimeHeadermodel.FromAddress = mimeHeadermodel.ToAddress = mimeHeadermodel.Subject = string.Empty;
mimeModel.MimeHeader = mimeHeadermodel;
return View("MailDetailsView", mimeModel.MimeBody.HtmlBody);
}
it's not showing the HtmlBody in the Iframe. But Its calling the controller. I dont know what I am missing.
Not sure if your using jquery or not, but:
$(function() {
var $frame = $('<iframe style="width:200px; height:100px;">');
$('body').html( $frame );
setTimeout( function() {
$.ajax(
url="/Home/GetMimeMessageType/small1.eml/HtmlBody",
type:'GET',
success: function(data){
var doc = $frame[0].contentWindow.document;
var $body = $('body',doc);
$body.html(data);
}
);
},1 );
});
I have not tested the above code, but this should work fine. A few things to note:
You will need to create a custom Route to map this to your controller:
http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
You will also need to change the javascript $('body').html() to be the ID of a div as a placeholder
also you will notice i have no path, if this never changes, you should add the path to your code or you can use formcollection and change the jquery ajax to a post, and set the variables and values there.
Your content will then up up in an iframe.
Forget the whole html.renderaction, this solution gives you a little more scope

mvc with ajax post

i have a form which includes list of records. When user clicks edit image on table modal div will show to him. i get this modal div with ajax. Now after changing some fields i am posting it via ajax. I watched to firebug. it sends parameter. But when i debug code in VS method calls but no parameter has been send. i have done it before in other pages. but now i can not. What problem can be here in my code?
C# Code here
[HttpPost]
//[Authorize(Roles = "Operator")]
public ActionResult EditRow(string Name, string SecondName)
{
//code goes here
return Content("Saved");
}
jquery ajax code is here
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'EditRow',
data: { Name: "php", SecondName: "MVC" },
dataType: 'html',
success: function (response) {
//some code goes here
}
});
Have you tried to simplify your Ajax call to:
$.ajax({
type: 'POST',
url: 'EditRow',
data: { Name: "php", SecondName: "MVC" },
success: function (response) {
//some code goes here
}
});
If this doesn't work there must be something else in your code that makes your code invalid. Maybe some base controller action filters you forgot to put there or some custom model binders or some other global registration.
Sending complex JSON to server
If you'd like to send complex JSON using the same technique, you can read my blog post and use the simple plugin that will make it possible to send complex JSON objects to Asp.net MVC controller action.

Resources