MVC razor syntax for accessing a static model member - c#

For MVC razor, what is the syntax on the .cshtml page to access a static model member (variable),
for example,
#Html.DisplayFor(#(myNameSpace.myClass.myStaticVarName))
I did this after being warned by the auto-complete to use the type specifier instead of an instance element.
However, the above throws a run-time error:
Compiler Error Message: CS1646: Keyword, identifier, or string expected after verbatim specifier: #

You cannot use the template helpers (Display, DisplayFor, EditorFor, etc.) because they all have a hidden parameter which is the model given to the view.
To display just the value of the class variable, use #myNameSpace.myClass.myStaticVarName

It's just assign it to a variable:
#{
var a = myNameSpace.myClass.myStaticVarName;
#Html.DisplayFor(m=> a)
}

If understood your question, Could it be "model.myStaticVarName"?

You shouldn't have 2 # on 1 line if the line starts with it. Remove the second #:
#Html.DisplayFor(myNameSpace.myClass.myStaticVarName)

Related

What is #Html.IsSelected(controller: "Dashboards")?

I am vb.net familiar. Have a razor c# piece of code. Went through pages of documentation but I cannot find out what does this code do?
<li class="#Html.IsSelected(controller: "Dashboards")>
I understand that runs function IsSelected of Html class but what is the meaning of argument passed to it?
if I understood correctly you want to under stand what this does
#Html.IsSelected(controller: "Dashboards")
# tells razor to output the following to the html code by executing
Html.IsSelected
the parameter is a Named or optional parameter. It is basically saying sets the value of parameter with name "controller" to "Dashboards"
The function's definition might be like
IsSelected(int notUsed = 0, string notUsed2 = null, string controller = "dead beef")
so that you can save yourself some typing instead calling IsSelected(0, null, "Dashboards")

using model in view, lambda expression explain

In the following code of the example application of VS2015:
#model LoginViewModel
<p>
#Html.DisplayNameFor(m => m.Name)
</p>
I want to know how the compiler knows that m refers to the LoginViewModel if I never said that m is an alias for LoginViewModel.
And I would like help understanding the lambda expression of DisplayNameFor. Why it requires a lambda if we could just pass the string m.Name? How is it used here?
The given Razor template will be compiled, so the Razor view compiler can do some magic here.
The compiler knows the type of your model because of the #model directive (without the #model directive the compiler falls back to dynamic).
If you look at the #Html.DisplayNameFor directive, then the Html instance is an object of the type HtmlHelper<TModel> where TModel is the type given by the #model directive. In your case is the concrete type HtmlHelper<LoginViewModel>.
Now the HtmlHelper<LoginViewModel>.DisplayNameFor method is stongly typed and the compiler can figure that 'm' (which is only a parameter name) is of type LoginViewModel and that the lamdba expression returns a value from the model.
During runtime the DisplayNameFor method is executed by providing your model object as parameter 'm' the expression returns the object of the model member (or the object the expression returns) and the MVC framework can inspect the object (Type, Validation Attributes, etc.) and produces the appropriate html based on internal or custom templates.
If you would just pass a string, then MVC would not be able to get the needed type and validation annotations (and much more information).
Your first question: It passes LogonViewModel to #Html.DislplayNameFor method since you have define it on you first line as your model (#model LoginViewModel)
Aslo as it mentioned here:
What is the #Html.DisplayFor syntax for?
Html.DisplayFor() will render the DisplayTemplate that matches the
property's type.
If it can't find any, I suppose it invokes .ToString().
DisplayNameFor is a strongly typed HTML helper. These were first introduced in ASP.NET MVC 2. The purpose of them (as per the linked article) is to...
...provide a nice way to get better type-safety within your view templates. This enables better compile-time checking of your views (allowing you to find errors at build-time instead of at runtime), and also supports richer intellisense when editing your view templates within Visual Studio.
If you want to understand the innards of how they work, the source code is available on GitHub.

How do I use DisplayNameForModel?

I tried using this as a header for a view but it returns an empty string. In the Razor layout, I have something like:
#model IEnumerable<MVCApp.Models.Model>
<h2>#Html.DisplayNameForModel()</h2>
Do I need to set something on the model definition itself? I tried a data annotation [Display(Name="Model Name")] but it is a build error:
Attribute 'Display' is not valid on this declaration type. It is only valid on 'method, property, indexer, field, param' declarations.
The documentation DisplayNameExtensions.DisplayNameForModel Method is terse. The syntax calls for a parameter, but says:
No overload for method 'DisplayNameForModel' takes 1 arguments
As the Usage section says "When you use instance method syntax to call this method, omit the first parameter"
So, how do I use this method to return something?
I just used the default MVC 5 template project in VS2013 and have the #Html.DisplayNameForModel() working with no issues.
First, you are using the wrong data annotation on your view model. You want to use [DisplayName("My Model Name")] and not [Display()]
[DisplayName("Test View Model")]
public class TestViewModel
{
public string TestProperty { get; set; }
}
Second, the html parameter you are seeing on MSDN is a required parameter for any Html helpers in MVC. You do not have to pass anything for this value, the view engine does this for you. So, in your view, you would use the following to get the Display Name that you set on the model as so.
<h2>#Html.DisplayNameForModel()</h2>
Now, your result should output the display name attribute you set in your html. *Note the Test View Model above the Log In text.
try
#Html.DisplayNameFor(m => m.Name)

syntax of using Viewbag in aspx view engine?

I know ViewBag is used in Razor view engine but not sure how to access viewBag's dynamic properties in aspx view engine.
As I want to put text inside Html.ValidationSummary() through Viewbag I am using the following syntax but getting error "has no applicable method named 'ValidationSummary' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax."
Syntex:
<%= Html.ValidationSummary(ViewBag.ValidationSummary)%>
It is complaining because ValidationSummary has no single parameter overload that takes a parameter of type dynamic; that is what ViewBag properties are. Cast the property to a string.
<%= Html.ValidationSummary((string)ViewBag.ValidationSummary)%>

ASP.NET MVC 2 DisplayFor()

I'm looking at the new version of ASP.NET MVC (see here for more details if you haven't seen it already) and I'm having some pretty basic trouble displaying the content of an object.
In my control I have an object of type Person, which I am passing to the view in ViewData.Model. All is well so far and I can extract the object in the view ready for display. What I don't get though, is how I need to call the Html.DisplayFor() method in order to get the data to screen. I've tried the following...
<%
MVC2test.Models.Person p = ViewData.Model as MVC2test.Models.Person;
%>
// snip
<%= Html.DisplayFor(p => p) %>
but I get the following message:
CS0136: A local variable named 'p' cannot be declared in this scope because it would give a different meaning to 'p', which is already used in a 'parent or current' scope to denote something else
I know this is not what I should be doing - I know that redefining a variable will produce this error, but I don't know how to access the object from the controller. So my question is, how do I pass the object to the view in order to display its properties?
N.B. I should add that I am reading up on this in my limited spare time, so it is entirely possible I have missed something fundamental.
TIA
Html.DisplayFor can be used only when your View is strongly-typed, and it works on the object that was passed to the View.
So, for your case, you must declare your View with the type Person as its Model type, (e.g. something.something.View<Person>) (sorry, I don't remember the exact names, but this should make sense), and then when calling Html.DisplayFor(p => p), p would take the value of the passed model (Person) value into the view.
Hope that made sense.
p is already a variable name; and variable names have to be unique throughout the current scope. Therefore displayFor(p=>p) is not valid, as you're declaring a new variable 'p' there. That way the compiler doesn't know wether to use your Person p, or the (p =>) variable.
So just rename it to
<%= Html.DisplayFor(person => person) %>

Categories