View how to get data and HTML helper methods from action

Source: Internet
Author: User
Tags actionlink

Way: 1 Use weak type fetch, 2, use strong type, the difference between the two is the way the view page is declared at the top

  If a weak type is used to accept data from the controller, there is absolutely no need to have any life on the view page, the data can be obtained from Viewdata,viewbag,tempdata, and the @model property can be passed on the page. Get the Viewdata.model data model from action. However, the @model data type will be object, so it is a weakly typed way of passing values.   If you use a strong type, you must use the @model keyword in the first row of the view page to introduce a data model specific to the view page eg: @model mvcapplication5.models.loginmodel @ Viewbag.title Use this approach to improve the development efficiency of the view, as you can use the Smart hints feature. ------------------------------- html Helper is used to assist the generation of HTML, in the development of the view will face a lot of HTML tags, processing these will appear cumbersome, in order to reduce the complexity of the view, you can use HTML helper method to help produce some HTML tags or content, Because these HTML tags are written in a fixed standard, wrapping them up as an HTML helper method allows view development to be faster and avoids unnecessary grammatical errors.  1, the output Hyperlink @html.actionlink ("AAA", "ActionName") is preset to link to the action of the name ActionName, because the controller name is not specified, So the controller that points to will be the same as the controller that this view page belongs to.   @Html. ActionLink ("AAA", "ActionName", "Controllername") is linked to the action  @Html under the specified controller. ActionLink ("AAA", " ActionName ", new {id=123,page=5}) sets an additional routevalue that can be passed in the third parameter to the data   @Html of type object. ActionLink (" AAA "," ActionName ", null,new {@class = "btnlink"}) when you need to pass in additional HTML attributes for hyperlinks, you can add parameters to the fourth parameter note: Because the class attribute name is used in HTML tags when applying CSS style categories, in C # The class is a keyword and cannot be used as an attribute of an anonymous object, so to see it as a property, you would add the @ jump symbol before it would compile correctly. In addition, if the HTML property of the output contains a minus sign, such as the Data-value property, the C # property name must be changed to an underscore instead, and the HTML property will automatically become a minus sign when it is output.   When using Html.ActionLink (), the first parameter is the display text of the hyperlink, which cannot be entered as an empty string, a blank string, or a null value, otherwise the exception is thrown thevalue cannot be null or empty.   But if we want to set a completely non-linked content, it will generally be used in the output image-based hyperlinks, because the designer wants to design a hyperlink to the image button, so the image is selected with a tag to output, and through the CSS and a background image such as this < tag a href= " /home/index "class=" Lnkbutton "></label a> such format output is not able to use Html.ActionLink to do, you must use the @url.action helper method, such as < tag a href. = "@Url. Action (" ActionName ")" class= "Lnkbutton" ></label a>  @Html. RouteLink ("Back to member area", new {type= "Default"}) @Html. RouteLink ("Return to member area", new {type= "default"},new {@class = "back"}) output as follows < label a href= "/account/login? Type=default "class=" Lnkbutton "> Back to Member Zone </label a>< label A  class="  href= "/account/login?type= Default "class=" Lnkbutton "> Back to member Area </tag a> You can also specify the route name in the route table routetable, and then add the additional RouteValue parameter @html.routelink (" Back to member area "," Member ", New {}) @Html. RouteLink (" Back to member area "," Member ", new {type=" default "}) @Html. RouteLink (" Back to member area "," Member ", New {type= "default"},new {@class = "back"})  2, output form   "" Produce table cell eg: @using (html.beginform ()) { < label P > Accounts: @Html. TextBox ("account") </label p>< tag p> password: @Html. Password ("Password") </label p>}  output content back to Yes < form form action= "/account/login" method= "POST" >< label p> account:< Tag input id= Text "value=" " /></label P>< label p> password:< label input id= "password" name = "password" type= "password" value= ""/></label P></form form>  Note: Of course we can also not apply @using, this is to ensure that the form label will be at the end of the use of the output form end tag, of course, can also be used at the end of the @html.endform () to close the label. However, BeginForm cannot directly pass the @ output and must be executed in C # statements. Eg:@{html.beginform ("Abount", "Home");} @Html. TextArea ("Date") @Html. TextArea ("MeMO") < tag input type= "Submit"/>@{html.endform ();   @Html. BeginForm () Preset output method is post, if you want to set get can enter a third parameter   @using (Html.BeginForm ("Search", "Home", Formmethod.get) {< Tag p> account: @Html. TextBox ("accounts") </tag p>}  If you want to upload on a form, you must add a Enctype property to the form label. And the content must be set to Multipe/form-data, to add additional properties through Html.BeginForm Helper must add the fourth parameter and pass in an anonymous object. Eg: @using (html.beginform ("Upload", "File", FormMethod.Post, new {enctype= "Multipart/form-data"})) {@Html. TextBox (" File1 "," ", New {type=" file ", size="} "< label input type=" submit "/&GT;} output is < form form action="/file/upload "enctype = "Multipart/form-data" method= "POST" >< label input type= "file" id= "File1"  name= "File1" size= "value=" "/ >< Label Input type= "Submit"/></form form>  Note: The HTML helper method does not have a file method, so it must be replaced with a TextBox. And pass in the third parameter to replace the built-in type property with theFile. Expansion: If you want to restrict the destination URL of the form to a specific routing rule, you can use HTML. Beginrouteform Auxiliary method specifies the route name eg: @using ("Inquiry", new {action= "Index"}) {}  "" (temporarily useless, later replenished)   "" The common form input field @html.password () is used to generate the password field, @Html. Hidden () is used to generate a hidden field, @Html. TextArea () The input box for generating multiline text, Used in a similar way to Html.textbox, but with more multiple loads you can specify rows and column values @html.dropdownlist () to produce a drop-down box eg: @Html. DropDownList ("list", viewdata["list"]as selectlist, "select")   @Html. The ListBox () is used to produce a multiple-selection drop-down menu, similar to the above usage. @Html. RadioButton () is used to generate a radio button (Name,value,true[false]) @Html. CheckBox () used to generate pick block,name,value  "" " Use the strongly typed helper method to add For,eg @Html to the previous name. Textboxfor () or @html.labelfor () so there are many advantages, But most importantly, at the top of the view page, you must define the reference data model for this page using @model, and if you do not declare it, you will not be able to use the strongly typed helper method eg: @Html. labelfor (model = model. Name)   @Html. ValidationSummary () Output form validation failure error message @html.validationmessagefor () is used to output the information content that should be displayed when an input validation error occurs in this field.

View how to get data and HTML helper methods from action

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.