ASP.Net MVC有一个Action渲染另一个Action

Cha*_*son 4 asp.net-mvc

我有两个页面需要,并希望显示url/index和/ review.两个页面之间的唯一区别在于评论我将有一个评论评论部分来显示和提交按钮.否则两页是相同的.我以为我可以为主要内容创建用户控件.

但是,如果我可以在Review操作下说,则标记以显示审阅内容并返回其余的索引操作.

你(通用的)你会怎么做?

Dan*_*son 6

模型示例

public class MyModel
{
  public bool ShowCommentsSection { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器动作

public ActionResult Index()
{
  var myModel = new MyModel();

  //Note: ShowCommentsSection (and the bool type) is false by default.

  return View(myModel);
}

public ActionResult Review()
{
  var myModel = new MyModel
  {
    ShowCommentsSection = true
  };

  //Note that we are telling the view engine to return the Index view
  return View("Index", myModel);
}
Run Code Online (Sandbox Code Playgroud)

查看(可能在index.aspx中的某个地方)

<% if(Model.ShowCommentsSection) { %>
  <% Html.RenderPartial("Reviews/ReviewPartial", Model); %>
<% } %>
Run Code Online (Sandbox Code Playgroud)

或者,如果Razor是你的一杯茶:

@if(Model.ShowCommentsSection) {
  Html.RenderPartial("Reviews/ReviewPartial", Model);
}
Run Code Online (Sandbox Code Playgroud)