0

So i have next app architecture:

  1. NavController, ListController
  2. _Layout, which consists of Menu(=PartialViewResult) from NavController and RenderBody section.

Typically requests go to ListController in RenderBody section.

Menu has search textbox. When user wants to search something, searchText goes to ListController as a parameter.

I want to place search text after clicking "find it" in the same textbox. How should i do it to make this workflow beauty?

I hope my picture will bring some clarity. Thanks.

appArchitecture

<!-- _Layout (approximate markup) -->
<html>
<head></head>
<body>
    @Html.Action("MenuLeft", "Nav")
    <div>
        @RenderBody()
    </div>
</body>
</html>


<!-- Menu PartialView -->
@using (Html.BeginForm("All", "List"))
{
    @Html.TextBox("SearchText", null) // searchText should be here
    <button type="submit"></button>
}


// Menu Controller
public class NavController : Controller
{
    public PartialViewResult Menu()
    {
        return PartialView("MenuPartial");
    }
}


public class ListController : Controller
{
    public ViewResult All(String searchText = null)
    {
        ...
        return View(model);
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Linerath
  • 13
  • 1
  • 2
  • "I want to place search text after clicking "find it" in the same textbox", that's not something you'll resolve in the backend. You need to use JavaScript for that, and I'd suggest one of the dozens of jQuery plugins that do this. You can search for "jquery autocomplete", for example – Camilo Terevinto Jan 06 '19 at 15:50
  • @CamiloTerevinto I mean place text as a model after page rerendering (because user clicked "find it") – Linerath Jan 06 '19 at 16:22

1 Answers1

0

Use View Models

_Layout can accept a model too, just like views and partial views.

One way to make use of this is to create a base ViewModel that has the properties you need in _Layout (or any of your partial views that are rendered by _Layout), and derive all other view models from this.

public class ViewModelBase {
    public string SearchText {get;}

    public ViewModelBase(string searchText) {
        SearchText = searchText;
    }
}

public class ListModel : ViewModelBase {
    public ListModel( ..., searchText) : base(searchText) {
        ...
    }
}

In _Layout, set the model type and you'll have access to the ViewModeBase properties and pass the searchText value in the call to render the menu.

<!-- _Layout (approximate markup) -->
@model ViewModelBase
<html>
<head></head>
<body>
    @Html.Action("Menu", "Nav", Model.SearchText)
    <div>
        @RenderBody()
    </div>
</body>
</html>

Update your NavController to accept the search text value:

// Menu Controller
public class NavController : Controller
{
    public PartialViewResult Menu(string searchText = null)
    {
        return PartialView("MenuPartial", searchText);
    }
}

Update your Menu PartialView to use a string model and set the value of the search box:

<!-- Menu PartialView -->
@model string
@using (Html.BeginForm("All", "List"))
{
    @Html.TextBox("SearchText", Model) // searchText should be here
    <button type="submit"></button>
}

I suggest once you get this working that you go back and give the Menu PartialView its own view model that contains a SearchText property.

View Models vs ViewBag

View models should be preferred over ViewBag. A full explanation can be found here, ViewModels or ViewBag?, but a quick summary of the benefits are:

  • Compile time checking
  • The ability to refactor with confidence
  • IDE support - such as the ability to navigate to all usages
  • Intellisense
Andy Vaal
  • 493
  • 4
  • 12
  • Ok, that's a solution. Thank you! But i found that ViewBag is more proper way to implement it :) – Linerath Jan 06 '19 at 17:31
  • Thank you for accepting. You can of course use ViewBag but I would advise against it. C# is a strongly typed language, benefits of which include catching type and property name errors at compile time rather than run-time, saving your time debugging. It can also make refactoring easier and help other developers to read, follow and use your code more easily. Using ViewBag circumvents these benefits. Further reading: https://stackoverflow.com/questions/13779294/viewmodels-or-viewbag/37296928#37296928 – Andy Vaal Jan 06 '19 at 17:56