Referencing class Syntax Razor C #

4

I have a class where I have saved a configuration key being key and value, however I need to get the value of that key and pass it to my cshtml page. I'm having trouble with this.

I've already passed the class path, but I can not reference to bring up the key value. I need to bring and value the key to be able to make a simple logic over that value.

I have a configuration class of keys that are saved: (key is ReportServer)

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

I have my page cshtml where it has 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>
        }
    } 
}

I have My business class: (I am getting the value of my key through 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 to 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.

    
asked by anonymous 21.07.2016 / 14:33

2 answers

4

A strongly typed ad-hoc solution would use tuples to pass more than one value to 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 in 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 ways

If you want a more encapsulated form, I recommend creating a view-model class that contains all of the data for that particular view ... so you would not need to use Tuple.

There is still another encapsulate if you want to pass a certain data to all views, or at least for most views: extend the WebViewPage class ... but that's a lot more work, really is the opposite of an ad-hoc solution such as tuple.

If it is a more elaborate case send a comment that I amplify the answer accordingly.

    
21.07.2016 / 14:58
3

I'll teach you two more sophisticated methods to get heavily typed values in your View .

Method 1: Deriving 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 reopen. You will see that @ReportServer can be added to your View without errors.

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

Usage:

@this.ReportServer()
    
22.07.2016 / 18:17