Techniques for adding dynamic output to Views

Source: Internet
Author: User
Tags actionlink

 

Rendering viewdata items using viewdata. Eval
One of the main uses for Inline code is to pull out and display data from viewdata, either
Treating it as a dictionary (e.g., <% = viewdata ["message"] %>) or as a stronugly typed object
(E.g., <% = model. lastupdatedate. Year %>). What you haven't seen yet is viewdatadictionary's
Eval () method, and how you can use it to scan for a value that might be anywhere in viewdata
Or model.
Eval () is a way of searching through the whole viewdata object graph-both its dictionary
And model object elements-using a dot-separated token syntax. For example, you might render
<% = Viewdata. eval ("details. lastlogin. Year") %>. Each token in the dot-separated
Expression is understood as either the name of a dictionary entry, or case-insensitively as
Name of a property. eval () recursively walks both the underlying dictionary and the Model
Object, in a particle priority order, to find the first non-null value. The previous example is
Capable of finding any of the following:
• Viewdata ["details. lastlogin. Year"]
• Viewdata ["details"]. lastlogin. Year
• Viewdata ["details. lastlogin"]. Year
• Viewdata ["details"] ["lastlogin"] ["year"]
• Viewdata. model. Details. lastlogin. Year
• Viewdata. model. Details ["lastlogin. Year"]
These are just a few of the specified possible ways it can resolve your expression. It will be actually
Check every possible combination of dictionary entry names and property names, firstly
On viewdata as a dictionary, and secondly on viewdata. Model, stopping when it finds a nonnull
Value.

 

 

 

<% = Html. Textbox ("mytext", "Val", new {someattribute = "someval"}) %>

The C # compiler doesn't exactly CT you to use C # reserved words as property names. So, if you try to render
A Class Attribute by passing new {class = "mycssclass"}, you'll get a compiler error (class is
Reserved word in C #). To avoid this problem, prefix any C # reserved words with an @ symbol (e.g., write new
{@ Class = "mycssclass"}). That tells the compiler not to treat it as a keyword. The @ symbol disappears
During compilation (as it's just a hint to the compiler), so the attribute will be rendered simply as class.

Table 10-3. html helpers for rendering links and URLs
Description Example
App-relative URL. Content ("~ /My/content.pdf ")
Output:/My/contentparameters
Link to named action/controller html. actionlink ("hi", "about", "home ")
Output: <a href = "/home/about"> Hi </a>
Link to absolute URL html. actionlink ("hi", "about", "home", "HTTPS ",
"Www.example.com", "anchor", new {}, null)
Output: <a href = "https://www.example.com/Home/
About # anchor "> Hi </a>
Raw URL for Action URL. Action ("about", "home ")
Output:/home/about
Raw URL for Route Data URL. routeurl (New {controller = "C", Action = ""})
Output:/c/
Link to arbitrary route data html. routelink ("hi", new {controller = "C", Action =
"A"}, null)
Output: <a href = "/c/a"> Hi </a>
Link to named route html. routelink ("hi", "mynamedroute", new {})
Output: <a href = "/URL/For/named/route"> Hi </a>
In each case other than URL. Content (), you can supply an arbitrary collection
Extra Routing Parameters in the form of a parameter called routevalues. It can be
Routevaluedictionary, or it can be an arbitrary object (usually anonymously typed) to be
Inspected for properties and values. The Framework's outbound URL-generation facility will
Chapter 10 N views 335

Either use those values in the URL path itself, or append them as query string values-
Example,
Html. actionlink ("Click me", "myaction", new {controller = "another", Param = "Val "})
May render the following, depending on your routing configuration:
<A href = "/Another/myaction? Param = Val "> click me </a>

drop-down list HTML. dropdownlist ("mylist", new selectlist (new [] {"A", "B"}),
"choose")
output:
choose A B
multiselect list HTML. listBox ("mylist", new multiselectlist (new [] {"A", "B"})
output:
A B

Link as Lambda expression html. actionlink Output: <a href = "/home/about"> Hi </a>
Mail-to link html. mailto ("e-mail me", "me@example.com", "subject ")
Output: <a href = "mailto: me@example.com? Subject = subject ">
E-mail me </a>
Submit button html. submitbutton ("submit1", "Submit now ")
Output: <input id = "submit1" name = "submit1" type = "Submit"
Value = "Submit now"/>
Submit image html. submitimage ("submit2 ","~ /Folder/img.gif ")
Output: <input id = "submit2" name = "submit2"
Src = "/folder/img.gif" type = "image"/>
URL as Lambda expression html. buildurlfromexpression Output:/home/about

 

HTML. beginform () renders opening and closing

tags (see the "rendering
form tags" section later in this chapter)
HTML. renderaction (), in Microsoft. web. MVC. DLL, performs an independent internal
HTML. renderroute () request, injecting the response into the current request's output.
see the "using HTML. renderaction to create reusable widgets
with application logic "section later in this chapter)
HTML. renderpartial () renders a partial view (see the "using partial views" section later
in this chapter)
HTML. validationmessage () renders a validation error message for a specific model property
(see the "validation" section in chapter 11)
HTML. validationsummary () renders a summary of all validation errors recorded (see the
"validation" section in chapter 11)
HTML. antiforgerytoken () attempts to block Cross-Site Request Forgery (csrf) attacks (see
the "Preventing csrf using the anti-forgery helpers" section in
Chapter 13)

Creating your own HTML helper Methods
There's nothing magical or sacred about the framework's built-in helper methods. They're just
. Net methods that return strings, so you're free to add new ones to your application.
For example, let's create a helper method that renders <SCRIPT> tags to import Javascript
Files. Make a new static class called myhelpers (perhaps at/views/myhelpers. CS ):
Namespace demoproject. Views
{
Public static class myhelpers
{
Private const string scriptincludeformat = "<SCRIPT src = \" {0 }\ "> </SCRIPT> ";
Public static string shortdescript (string virtualpath)
{
String clientpath = virtualpathutility. toabsolute (virtualpath );
Return string. Format (scriptincludeformat, clientpath );
}
}
}

<% = Demoproject. Views. myhelpers. includescript ("~ /Scripts/somescript. js ") %>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.