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

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")

Related

Syntax Error Passing Razor Markup As Parameter

I'm trying to write a custom control and associated html helper that can have it's output changed by providing a template. I've found examples of this on the web (example) but keep hitting upon a strange syntax error that I don't understand.
Here's a simplified version of what I've got so far:
Method signature that takes the razor template
public IHtmlString Template<T>(Func<T, object> template) {
// Implementation not important
}
Calling the method in my view:
#Html.Control(Of Integer)().Template(#<div>#item</div>)
However, I keep getting a syntax error which is showing on the closing bracket of the Template method. I can't figure out what I'm doing wrong here nor could I find any similar problems else where. Does anyone have any ideas what's going wrong here?
Also yes I realise that the helper is written in c# and the razor is in vb sadly that's the way things are round here.

StringTemplate use object property in format of renderer call

I'm referencing c# resource strings from my template (which uses $ as delimiters). I'm doing so by passing in as one of the arguments to the template the object "strings", which is a c# ResourceManager. The formatString is expected to be the name of the resource desired. This allows me to put in my template for example:
<Name>$strings;format="ItemName"$</Name>
The only issue with this solution is, there are times I want something from my object tree to help build the name of the resource string to look up. For example, a generic lookup for a localized value for an enum could be provided with:
<Type>$strings;format="ItemType$myItem.Type$"$</Type>
However - when I attempt to call the renderer this way, if I use the above syntax, the myItem.Type is not resolved into its value, and instead passed directly as part of the string, so I get the value "ItemType$myItem.Type$" rather than say, "ItemTypeBicycle".
If I don't place quotes around it however, such as:
<Type>$strings;format=ItemType$myItem.Type$$</Type>
I get a compile-time error that this "doesn't look like an expresson"
Is there another syntax I'm missing, or are you not able to resolve something that is then passed as a string like this?

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)

MVC razor syntax for accessing a static model member

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)

Html.ActionLink() not working for passing a C# object

I'm trying to pass an object through an ActionLink to a method in an MVC controller.
The razor syntax:
#Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",new { SearchObject = Model.SearchObject})
What's actually being displayed in markup:
Export to Excel
The controller method is being called just fine, hence no reason to post here. What needs to be done so that the actual values are passed into the actionLink instead of DTO.SearchObject? According to HTML.ActionLink method it looks like I have the right syntax (using MVC 4).
You should be able to pass the DTO, assuming it's just that, as the parameter into ActionLink:
#Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",Model.SearchObject)
Any public fields will be added as a query parameter key/value pair.

Categories