2012-12-04 8 views
5

を返すASP.NET MVCビューを決定する私のMVCサイトはモバイルブラウザと非モバイルブラウザでうまく動作します。私が持っている問題はこれです。私はいくつかのアクションがある(ログの理由のために)return RedirectToAction(...);を代わりに使用したくないので、return View("OtherView", model);モバイルで試してみるまで、これはうまく動作し、OtherView.Mobile.cshtmlが見つかりませんでした。この仕事をする方法はありますか?返されたビュー(モデル)を使用せずに返されたView( "viewName"、model)

Thiseは図で

Views/Account/Create.cshtml 
Views/Account/Create.Mobile.cshtml 
Views/Account/CreateSuccess.cshtml 
Views/Account/CreateSuccess.Mobile.cshtml 

これは

public ActionResult Create(FormCollection form) 
{ 
    TryUpdateModel(model); 

    if(!ModelState.IsValid) { return View(); } // this works correctly 

    var model = new Account(); 

    var results = database.CreateAccount(model); 

    if(results) return View("CreateSuccess", model); // trying to make this dynamic 

    return View(model); // this works correctly 
} 

は、通常、私はちょうど、アカウントの詳細ページへreturn RedirectToAction(...);どうなるアクションであるが、これは、このユーザーのための追加のログエントリを(生成されます。読み取り中)、詳細ページにはパスワードへのアクセス権がありません。 ActionResult Createはもともとはパスワードを持っていたので、確認のためにユーザに表示することはできません。私はちょうど彼らの最初の使用上のセッション変数を設定します

Views/Account/Create.cshtml 
Views/Account/Create.Mobile.cshtml 
Views/Account/Create.iPad.cshtml 
Views/Account/CreateSuccess.cshtml 
Views/Account/CreateSuccess.Mobile.cshtml 
Views/Account/CreateSuccess.iPad.cshtml 

答えて

0

:私はモバイルiPad用のためのビューまたは任意の別のセットを追加してしまう可能性があるため、

明確にするために、私はif (Request.Browser.IsMobileDevice) mobile else fullを行うにはしたくありませんサポートされているすべてのビューを識別する「配信タイプ」になります。

public enum DeliveryType 
{ 
    Normal, 
    Mobile, 
    Ipad, 
    MSTablet, 
    AndroidTablet, 
    Whatever 
} 

次にあなたが見るには、「アドオン」の配信にどこか

public DeliveryType UserDeliveryType 
{ 
    get 
    { 
     return (DeliveryType)Session["DeliveryType"]; 
    } 
    set 
    { 
     Session["UserDeliveryType"] = value; 
    } 
} 

あなたも別の方法に入れることができますプロパティまたは拡張メソッドを持つことができます。そして、

public string ViewAddOn(string viewName) 
{ 
    return (UserDeliveryType != DeliveryType.Normal) ? 
     string.Format("{0}.{1}", viewName, UserDeliveryType.ToString()) : 
     viewName; 
} 

あなた最終コールは:

if (results) return View(ViewAddOn("CreateSuccess"), model); 

次に、すべての配信タイプに対応するビューがあることを確認するだけです。一致するビューがあることを確認するために何らかの種類のチェッカーを作成し、標準ビューを返さない場合は賢明な方法があります。

0

ViewData変数を持つPartialを持つ疑似ビューを作成できます。 @ Html.Partialは正しいビューを見つけます。あなたは今CreateSuccess.cshtmlとCreateSuccess.Mobile.cshtmlを持つことができます

CustomView.cshtml: 
if (ViewData.ContainsKey("PartialViewName")) { 
@Html.Partial(ViewData[PartialViewName]); 
} 

Controller.cs: 
public ActionResult Create(FormCollection form) 
{ 
    TryUpdateModel(model); 
    ViewData[PartialViewName] = "CreateSuccess"; 
    if(!ModelState.IsValid) { return View(); } // this works correctly 

    var model = new Account(); 

    var results = database.CreateAccount(model); 

    if(results) return View("CustomView", model); // trying to make this dynamic 

    return View(model); // this works correctly 
} 

:このような

何か。

注:すべてのアプリケーションで1つのCustomeView.cshtmlが必要です。

注2:あなたは、常にviewbagのような別の方法でパラメータを渡す、またはあなたがより快適に感じさせるものは何でもテクニックができます:D

それは解決策よりもハックのより少しです。あなたがもっときれいなものを思いついたのかどうか教えてください。

関連する問題