简体   繁体   中英

MVC4 ASP.NET Mobile contacts form redirect

I am attempting to make my site mobile ready. I am doing it by creating a duplicate index page called Index.Phone. Its going well. Most things are detected properly apart from when I submit my contacts page using mobile. Once contacts form is submitted I am trying to redirect to a thank you page. I can do this with desktop without any problem. When I use a Mobile to do it it redirects to my EmailSent.cshtml (desktop page) and not my EmailSent.Phone.cshtml. I believe it is due to the redirect from the controller and not from the client which I believe would use global.asax that I have sorted out.

This is the HomeController action which does get called.

public ActionResult Contact(ContactModel pContactModel)
    {
        if (ModelState.IsValid)
        {

            bool myBool = SendEmail(pContactModel);
            if (myBool == false)
            {
                TempData["emailSent"] = "false";
                return RedirectToAction("Contact");
            }
            else
            {                    
                    return RedirectToAction("EmailSent");  
            }
        }
        return View();
    }

This is my EmailSent.cshtml for desktop...

@{
Layout = "~/Views/Shared/_Layout.cshtml";
Page.Title = "desktop";

}

This is my EmailSent.Phone.cshtml which I am trying to invoke with the above redirect.

   @{
Layout = "../Shared/_Layout.Phone.cshtml";
Page.Title = "mobile";

}

Thank you for any help you give me. I have omitted the body of both files to keep it simple.

What you could do is just make your css conditional based on the @screen size or you can go by the browser. You would have to somehow create a condition to check what type of browser you have in order to render a specific view, so I would try to check what type of device/browser I am working with through css

I can't guarantee this is the fix, but...

There is a bug in MVC 4 RC and RTM that caused a problem with the view caching engine. After around 15 minutes the DefaultDisplayMode would be ignored. Read more about it here: http://blogs.msdn.com/b/rickandy/archive/2012/09/17/asp-net-mvc-4-mobile-caching-bug-fixed.aspx

You need to make sure you install version 1.0.1 of this Nuget package to fix this bug: http://www.nuget.org/packages/Microsoft.AspNet.Mvc.FixedDisplayModes/1.0.1

You can do this using Package Manager Console and running:

Install-Package Microsoft.AspNet.Mvc.FixedDisplayModes -Version 1.0.1

Essentially thats the problem solved. Thank you.

public ActionResult EmailSent() { return View(); }        

    [HttpPost]
    public ActionResult Contact(ContactModel pContactModel)
    {
        if (ModelState.IsValid)
        {        
            bool myBool = SendEmail(pContactModel);
            if (myBool == false)
            {
                TempData["emailSent"] = "false";
                if (Request.Browser.IsMobileDevice) { return View("Index.Phone"); } 
                else { return RedirectToAction("Contact"); }
            }
            else
            {
                if (Request.Browser.IsMobileDevice) { return View("EmailSent.Phone"); } 
                else { return RedirectToAction("EmailSent"); }
            }
        }
        else
        {
            if (Request.Browser.IsMobileDevice) { return View("Index.Phone"); }
        }

        return View();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM