1
\$\begingroup\$

I have one input that is needed in many forms, so I created a partial view for that. In the partial view I query the database and generate a drop down list.

I just want to make sure, this is the best way to achieve this.

The partial view looks like this:

@model long?


@{
    var data = Utility.GetData();
}

@Html.DropDownListFor(m => m, new SelectList((System.Collections.IEnumerable)data, "Id", "Text"),
    new { @class = "form-control input-sm" })

And I'll use it like this:

@Html.EditorFor(x => x.LiIdPeriod, "Years")

I should note that this is a SPA and intranet application.

\$\endgroup\$
  • 3
    \$\begingroup\$ Casting to a System.Collections.IEnumerable? Why not make Utility.GetData() return the "proper" type? Also, I'd advise you use a Model and a ViewModel etc., like in this example. \$\endgroup\$ – BCdotWEB Jul 8 '15 at 7:38
  • \$\begingroup\$ Thanks, I've changed the function to return IEnumerable. I'm using a ViewModel in my views, but different models have this particular field, so I've decided to use long model for this partial class. \$\endgroup\$ – Akbari Jul 9 '15 at 5:46
  • \$\begingroup\$ There is another side question too! What do you call the Utility class!? It has many functions that all of them retrieve some kind of information from database and return it in a particular format. \$\endgroup\$ – Akbari Jul 9 '15 at 5:57
  • \$\begingroup\$ @Akbari - Utility does too much. \$\endgroup\$ – JᴀʏMᴇᴇ Jul 9 '15 at 13:03
3
\$\begingroup\$

I'm new to MVC (relatively), but I don't think I'll ever plan on doing too much in a view. In my understanding, it's merely a template for an object (view model) you throw at it. There shouldn't be much server-side functionality in there at all. Getting data from a database and selecting a SelectList from this collection is too much. There isn't enough separation between concerns of presentation and functionality.

Get that database call into a controller (or better still, a business layer) and keep your views simple. Then you can decide whether you want to call this new action asynchronously or not.

\$\endgroup\$
  • \$\begingroup\$ I don't really get your opinion, can you provide an example or link please? \$\endgroup\$ – Akbari Jul 9 '15 at 5:59
  • 1
    \$\begingroup\$ Well the abbreviation MVC itself is an example. Model, view and controller. You have a model (an object of data to be fed to the view), a view (merely a template of markup to display this model), and a controller, which is to bring the two together with some server-side functionality. What you've proposed involves functionality and construction of a model, all in the view. That's not right. That's not separated out enough. Not only is it not pragmatic, but it'll be trickier to maintain and near impossible to reuse without a copy a paste job. \$\endgroup\$ – JᴀʏMᴇᴇ Jul 9 '15 at 8:27
1
\$\begingroup\$

No that is not the best way to achieve this.

If this is an SPA then the data should be retrieved in an AJAX call in my opinion.

However if you want to include the data in the SPA template, then this partial view should not be gettings it's own data. You should be passing a model or viewmodel to it.

\$\endgroup\$
  • \$\begingroup\$ I was following that approach, but it was too messy. There are other fields like this too. So I should always check if the ViewData or ViewBag has the required information or not. And I had to change all of the views to include those information, in contrast to this approach that I don't need to worry about the data and other things. Why do you suggest that? \$\endgroup\$ – Akbari Jul 9 '15 at 5:52
  • \$\begingroup\$ Thanks for your replay, why do you suggest using an Ajax call? Any particular benefit? \$\endgroup\$ – Akbari Jul 9 '15 at 5:53
1
\$\begingroup\$

Do not access your database in your views. You should use a view and viewmodel to separate the tiers. You can pass your viewmodel to your partial view. You are right, that you have to extend your existing view models. You could use a base view model and inherit this view model in your other view models which need the enumerable list.

You could also write a HTML Helper method, but I would prefer the partial view and viewmodel solution (better seperation of code and user interface).

As mentioned your GetData() method should return a enumerable type.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.