URL issue in MVC4 Mobile website (Razor View Engine) with AJAX - ajax

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.

Related

ASP.NET MVC jQuery mobile like pages mechanism

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!

How to create ajax crawable in asp.net

How to create token "#!" in asp.net mvc?
How to create html snapshot?
I have read google document, They talk create "Indicate to the crawler that your site supports the AJAX crawling scheme" by www.example.com/ajax.html#key=value to change www.example.com/ajax.html#!key=value.
But in asp.net mvc 4 I can't how to create "#!" because when i use #Ajax.ActionLink("link text", "controller", new { attribute router here },new AjaxOptions{ HttpMethod="GET", InsertionMode= InsertionMode.Replace, UpdateTargetId ="id to add function of ajax"}). So it gernater "action/?key=valuer" not like i expected "action/#!key=value"
So somebody help me. I want to google crawble link when ajax function do when i click a text and ajax function excute
what is html snapshot, is this create after when i excute ajax function( click text and ajax function do gernater hmtl to UpdattagetId(I code in asp mvc). So i can dont change any thing with existing i haved code: Ajax.actionlink(...) and gernerater html to UpdatetargetId)

Handling redirects with Codeigniter and jQuery mobile?

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.

mvc application with razor data view engine problems with the url

I create a new mhc application with razor data view engine. I have a problems with the url
Here is my action links
#Html.ActionLink("Home", "Index")
#Html.ActionLink("Schedule", "Schedule")
After I loaded home page my url looks fine
Example: mysiteurl.com
Then I click Schedule link (if I hover I see the correct url http://mysiteurl.com/home/schedule). If I click it as a result my url will http://mysiteurl.com//#/Home/Schedule. I don't know why its adding # sign to my url but it's causing the issue in my application with other pages.
any idea what I'm doing wrong?
Do you have an action method named Schedule? Can you successfully hit it by referencing yoursite.com/Home/Schedule? If not make sure your route is correct.

ASP.NET MVC3 - Action without reload site

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());
}

Resources