5

私の mvc サイトは、モバイル ブラウザーと非モバイル ブラウザーで正常に動作しています。私が抱えている問題はこれです。(ログの理由から) 実行したくないアクションがいくつかあるので、モバイルで試すまでこれreturn RedirectToAction(...);を使用していましたが、OtherView.Mobile.cshtml が見つかりません。return View("OtherView", model);これを機能させる方法はありますか?

これがビューです

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、二度と見られないようにする前に、確認のためにそれをユーザーに表示できます。

明確にするために、私はやりたくありません。if (Request.Browser.IsMobileDevice) mobile else fullなぜなら、iPad などのために別のモバイル ビューのセットを追加することになるかもしれないからです。

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
4

2 に答える 2

0

最初の使用時に、サポートされているすべてのビューを識別する「配信タイプ」となるセッション変数を設定するだけです。

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

次に、配信タイプごとに対応するビューがあることを確認するだけです。ある種のチェッカーを作成して、一致するビューがあることを確認し、そうでない場合は標準ビューを返すことが賢明な場合があります。

于 2012-12-04T21:21:06.293 に答える
0

ViewData 変数を持つ Partial を持つ疑似ビューを作成できます。@Html.Partial は正しいビューを見つけます。

このようなもの:

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
}

CreateSuccess.cshtml と CreateSuccess.Mobile.cshtml を使用できるようになりました。

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

注2:ビューバッグのような別の方法でいつでもパラメーターを渡すことができます。または、より快適に感じるテクニックは何でも:D

それは解決策というよりもちょっとしたハックです。もっとかわいいものを思いついたら教えてください。

于 2013-04-01T17:24:22.527 に答える