I have MVC3 app with form for edit data. This for include only dropdownlist.
It is any posibility to execute Controller method (HttpPost) without reload?
If not - how Can I return current site (because I have the same form in different sites).
Yes, try XmlHttpRequest better known as AJAX to execute controllers. It's like sending a request from the browser to the server on a background thread which won't cause a page reload.
have a read of these blogs:
http://www.nikhilk.net/Ajax-MVC.aspx
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-2-0-and-AJAX-Part-1.aspx
If you can't use ajax, to get back to the same page you can redirect the user back to the referrer url:
public ActionResult Submit()
{
// do something
return Redirect(Request.UrlReferrer.ToString());
}
Related
To give a more appish experience to my asp.net mvc application, I would like to make the links to other pages (actions in mvc) be loaded using ajax (displaying a loading sign while the request is being processed). I have been quite successful doing this with jQuery Mobile but for the project that I am doing now I prefer not to use it. In part this is because as soon as loading jQuery Mobile css the previous look of the site is disrupted.
Is there a similar page mechanism in asp.net? It would be even better if features such as transitions and pre-rendering of pages would be available too, as jQuery Mobile does.
An example of what I try to achieve
I have a controller like this:
public ActionResult List()
{
return View();
}
public ActionResult Dashboard()
{
return View();
}
Then on the dashboard view a link:
LIST
Clicking the link would request the list page from the server and start a slide effect to transition from one page to the other.
Thanks for your ideas and knowledge on that!
An ajax request calls a controller function within my MVC Component.
And in this function there is a Joomla redirect at the end with
JFactory::getApplication()->redirect(JRoute::_($redirect_url, false));
The problem is, that the redirected page won't open! While debugging a session, I can see that the controller will initiate the redirect and the proper details will be sent to view.html.php. But the page, in this case it is a different view in tmpl, will not be open!
Is this because of the ajax call? Do I have to do something in the ajax success part?
Yes, now I do the url preparation in the controller and send back the url to ajax.
$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout"
Ajax will do the redirect with the given url.
window.location = result.data;
From there iam struggeling with the SEF part of Joomla to get a SEF friendly URL.
I'm building a mobile web app with jQuery mobile and Codeigniter & TankAuth. When a user logs in or out, they are redirected from the TankAuth login/logout controller to a page of my choice. The problem is that while the ajax navigation loads the page in question, the URL to the TankAuth controller stays the same.
There is quite a lot of code involved, so I will describe it the best I can:
Consider the variable $this->session->flashdata('prev_page') is page1.
The TankAuth login controller is called first time via auth/login, loads login form view.
The form is submitted to the same controller which then processes it and redirects to necessary page:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {// success
redirect($this->session->flashdata('prev_page'));
}
jQuery mobile loads page1 via AJAX but the URL remains as auth/login.
How do I fix this? Please let me know if you need any more info.
If my understanding about your problem is correct, you are using ajax for authenticating mobile users and normal redirect method for big screen users.
If that's the situation, then this will do the magic:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email']))
{// success
$this->load->library('user_agent');
if($this->agent->is_mobile()) {
echo $this->session->flashdata('prev_page');
//Now this value 'll reach ajax when authentication is success.
//Inside Ajax:
//success: function(data) {
// validate data for checking if user is logged in or not.
// if(login_is_success)
// window.location.href = data;
//}
}
else
redirect($this->session->flashdata('prev_page'));
}
I added data-url="true" to the the form in opening <form> tag in question, everything worked fine from there.
When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}
I am developing a mobile website in MVC4 (using Razor view Engine).
The issue I am facing is that When I navigate to different View using RedirectToAction() method, the new view is served but the URL remains the same in the browser.
I searched about it and found that If I disable the AJAX on my page (by setting attribute [data-ajax=false]) then It start working fine(i.e. correct URL is displayed). But Ajax stops working.
But my problem is that I can not disable AJAX for mobile website & I need to display correct URL for each page.
EDIT:
[HttpPost]
public ActionResult Search(string btnSelection)
{
//Some code here...
//Then redirect to another View using following command:
return RedirectToAction("SelectTask", "Task");
}
But to achieve proper URL display in MVC4 mobile, I have to set following attribute in View:
#using (Html.BeginForm("Search", "Task", FormMethod.Post, new { data_ajax = false }))
But this stops the AJAX. Any ideas how to fix this using AJAX?
Please help if you can asap.
Thanks.
Ajax is not for navigating. You must submit form if you have data to save or use a link (e.g. #Html.ActionLink("Home", "Index", "Home")) to redirect to the other view.