【问题标题】:Point actions to the same view将动作指向同一视图
【发布时间】:2011-01-12 15:21:54
【问题描述】:

我有一个具有以下操作的控制器...

public ActionResult YellowList()

public ActionResult RedList()

public ActionResult BlueList()

所有这些操作都填充相同的视图模型 (ListViewModel)。

如何让它们都指向同一个视图 (aspx)?

谢谢,

ETFairfax。

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    在你的操作中使用这个视图方法的重载:

    public ActionResult YellowList()
    {
        ListViewModel model = GetSomeModel();
        return View("viewName", model);
    }
    
    public ActionResult RedList()
    {
        ListViewModel model = GetSomeModel();
        return View("viewName", model);
    }
    
    public ActionResult BlueList()
    {
        ListViewModel model = GetSomeModel();
        return View("viewName", model);
    }
    

    【讨论】:

    • 优秀。我之前确实尝试过,但是没有用。根据您的建议再次尝试,这次成功了!!!用户错误!!!!非常感谢您的回答。
    【解决方案2】:

    这里是控制器:

    public class ColorsController : Controller
    {
        //
        // GET: /Colors/
    
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Red()
        {
            ViewData["Color"] = "<span style=\"color:Red\">Red</span>";
            return View("ColorName");
        }
    
        public ActionResult Green()
        {
            ViewData["Color"] = "<span style=\"color:Green\">Green</span>";
            return View("ColorName");
        }
    }
    

    这里是视图:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <h2>ColorName</h2>
        <h3>You Selected: <%= ViewData["Color"] %></h3>
    </asp:Content>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多