4

I have two pages I need, and want to show for the url /index and /review. The only difference between the two pages is on the review I will have a review comment section to show and Submit button. Otherwise the two pages are identical. I thought I could create a user control for the main content.

However, if I could say under the Review action, flag to show review stuff and them return the rest of the index action.

How would you (generic you) do this?

3 Answers 3

6

Model example

public class MyModel
{
  public bool ShowCommentsSection { get; set; }
}

Controller actions

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

View (somewhere inside your index.aspx probably)

<% if(Model.ShowCommentsSection) { %>
  <% Html.RenderPartial("Reviews/ReviewPartial", Model); %>
<% } %>

Or, if Razor is your cup of tea:

@if(Model.ShowCommentsSection) {
  Html.RenderPartial("Reviews/ReviewPartial", Model);
}
Sign up to request clarification or add additional context in comments.

Comments

4
// ... Make comment section visible
return Index();

Comments

0

Why don't you just always include the review comment form and use client side code to show or hide it. Considering no additional data is required for the review page beyond that of which already needed on the index a round trip to a controller is not necessary. This would allow you to delete the review action and view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.