【问题标题】:Determine which ASP.NET MVC View to return when not using return View(model) but return View("viewName", model)确定在不使用 return View(model) 而是 return View("viewName", model) 时返回哪个 ASP.NET MVC 视图
【发布时间】:2012-11-22 14:36:05
【问题描述】:

我的 mvc 网站在移动和非移动浏览器上运行良好;我遇到的问题是这个。我有几个操作(出于日志记录的原因)我不想在上面执行return RedirectToAction(...); 所以我一直在使用return View("OtherView", model); 这一直有效,直到我在移动设备上尝试它,它没有找到 OtherView.Mobile .cshtml。有没有办法让这个工作?

这是风景

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc mobile views


    【解决方案1】:

    您可以创建一个伪视图,其中包含一个带有 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。

    note2:你总是可以用另一种方式传递参数,比如 viewbag,或者任何让你感觉更舒服的技术:D

    与其说是解决方案,不如说是一种技巧。如果您想出了更漂亮的东西,请告诉我们。

    【讨论】:

      【解决方案2】:

      我会在他们第一次使用时设置一个会话变量,它是一个“交付类型”,用于标识所有支持的视图。

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

      那么您只需要确保对于每种交付类型都有相应的视图。谨慎的做法是构建某种检查器来验证您是否有匹配的视图,如果没有则返回标准视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多