Reference class Syntax Razor C#

Asked

Viewed 650 times

5

I have a class where I have saved a configuration key being key and value, but I need to take the value of this key and move to my page cshtml. I’m having a hard time with this.

I have already passed the class path, but I can not reference to bring the value of the key. I need to bring and value the key so I can make a simple logic upon that value.

I have a key configuration class that is saved: (key is Reportserver)

public static string ReportServer
{
    get { return InstanceResolverFor<IConfigurationSource>.Instance.Read("ReportServer"); }
}

I have my page cshtml where you have this Razor syntax:

@section scripts{
    @Scripts.Render("~/Content/js/app/FIDC/index.js")
    @if (Model != null)
    {
        if (Model.Erro)
        {
            <script>
                modal.exibir("Ops", "@Model.MensagemErro", modal.tipoModal.Erro);
            </script>
        }
        else
        {
            <script>
                modal.exibir("Ok", "@Model.MensagemSucesso", modal.tipoModal.Sucesso);
            </script>
        }
    } 
}

Got My Business Class: (I’m taking the value of my key through the config.ReportServer)

 var serverReport = reportViewer.ServerReport;
 serverReport.ReportServerUrl = new Uri("http://CCD-APPBI-001:80/ReportServer");
 serverReport.ReportPath = string.Concat("/", config.ReportServer, "/",relatorio);

Now I need to get the value of this key ReportServer for my html, where I have two tags below:

<a target="_blank" href="http://ccd-appbi-001/Reports/Pages/Report.aspx?ItemPath=%2fRelatoriosClientes%2fcessao_polo&SelectedSubTabId=GenericPropertiesTab&SelectedTabId=ViewTab;rs:Command=Render">Abrir</a>

<a target="_blank" href="http://ccd-appbi-001/Reports/Pages/Report.aspx?ItemPath=%2fRelatoriosClientes%2fsabemi&SelectedSubTabId=ReportDataSourcePropertiesTab&SelectedTabId=ViewTab">Abrir</a>

If anyone can help, thank you.

2 answers

5

A very ad-hoc and strongly typed solution would be use tuples to pass more than one value for the cshtml view:

Example - I will pass 2 data to CSHTML (the view model and a string representing the key):

In the view do so:

@model Tuple<MinhaClasse, string>

<p><strong>Nome:</strong> @Model.Item1.Nome<p>
<p><strong>Chave:</strong> @Model.Item2<p>

And on the controller:

public ActionResult Metodo()
{
    MinhaClasse viewModel = ...; // esse é o seu View-Model
    string chave = ...; // essa é a chave que quer passar pra view

    return this.View(Tuple.Create(
        viewModel,
        chave
      ));
}

Other forms

If you want a more encapsulated form, I recommend creating a view-model class that contains all the data for that specific view itself... so you wouldn’t need to use Tuple.

Still there is another were even more encapsulated if you want to pass a certain data to all views, or at least to most views: extend the class WebViewPage... but there is much more work, really is the opposite of an ad-hoc solution like the tuple.

If it is a more elaborate case send a comment I expand the response accordingly.

  • Thanks Miguel, however my problem is that the key already has a value that is saved in a bank nosql that lives within my application, Summarizing: in Homologation the key will point reports to homologation and Production the key will point report to production.

  • So. You plug into the no-sql database, load the key into the variable chave and passes it to the cshtml view the way I indicated.

  • Usually all the logic of doing database queries is in the controller, exactly so that the view is just a simple layer of visualization.

  • I’ve changed my mind. I’ll answer, because the question now is completely different.

4

I will teach you two more sophisticated methods to obtain strongly typed values in your View.

Method 1: By derivation System.Web.Mvc.WebViewPage

Create something like this:

namespace SeuProjeto.Infrastructure.ViewPages
{
    public abstract class MinhaWebViewPage : WebViewPage
    {
        public string ReportServer { get; set; }
    }

    public abstract class MinhaWebViewPage<TModel> : WebViewPage<TModel>
    {
        public string ReportServer { get { return // defina o valor aqui } }
    }
}

In Views/Web.config (not the root directory), change the following:

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <!--<pages pageBaseType="System.Web.Mvc.WebViewPage">-->
  <pages pageBaseType="SeuProjeto.Infrastructure.ViewPages.MinhaWebViewPage">
    ...
  </pages>
</system.web.webPages.razor>

Close your Views and reopens. You will see that @ReportServer may be added to your View error-free.

Method 2: Extending System.Web.Mvc.WebViewPage

Create something like this:

namespace SeuProjeto.Infrastructure.Extensions
{
    public static class WebViewPageExtensions
    {
        public string ReportServer(this System.Web.Mvc.WebViewPage webViewPage)
        {
            return // retorne seu valor aqui
        }
    }
}

Use:

@this.ReportServer()
  • 1

    In method 1, it is still possible instead of using Web.config + @model directive, to use the @inherit directive of Razor indicating the class MinhaWebViewPage<TModel>. Making the solution specific per file, if it is of interest to OP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.