How to send a background-encapsulated Web control to the foreground view display in MVC

Source: Internet
Author: User


Recently in the development of MVC, the elder sister said that all the controls used to write in the background, and then from the background to the foreground, so that the previous page has changed the shortcomings of the dead. But at the time of implementation, there were some problems,




The above code wrote a LinkButton, when called this static method, will generate a LinkButton HTML code, this is a relatively simple component, if, now I want to splice a full-featured DataGridView control, to this control has a page bar, Various add delete function, CSS style, in order to reuse our Easyui component library, it is estimated that this HTML code will have to have more than half a page. If I want to truncate the character every time I encounter a parameter, and I want to add an escape character to the quotation marks inside the code, I feel rather troublesome. So, is there a way of not truncating characters?



Now recall how we used to avoid SQL injection on layer D when we were studying layer three, and we introduced parameters to avoid stitching characters. According to this idea, we can also define parameters in the string, and then actually call the time, and then replace the placeholder with the parameters.


here, we use a replace method of string, and then use the substitution of the string to pass in the parameter.



Let's look at the demo:



First of all, because it is to do the demo, so many of the layered things have been simplified by me, only to be logically realized.



Here is the controller class and the code that generates the simple form:


need to carefully understand the next replace method, there is no sense that this is another variable assignment of the expression?


    public class Testcontroller:controller {//Create a control's HTML code public String Convertsingleform (Str ing singleformid, string singleform_table_id, String method)//The parameter is the formid,tableid,form of the submission method//The above parameter is a string, used to replace the HTML generation The value of the attribute in the code {//the HTML string that establishes the control, where all quotation marks are replaced with "replace;//Use" for two reasons://1, in the back REPL Ace is converted//2, "is a special character in HTML and will be automatically parsed into quotation marks String htmlsingleform = @" <form id= "Singleform "Method=" POST "><table id=" singleform_table_id "bgcolor=" 00ff99 "><tr> <td> password </td> <td >3455467547556634568568568643</td> </tr><tr> <td> Password </td> <td>3455467547556           634568568568643</td> </tr></table></form> ";   Htmlsingleform = Htmlsingleform.replace ("" "," \ "");   Convert Quotes Htmlsingleform = htmlsingleform.replace ("Singleform", Singleformid); Formid Assignment Htmlsingleform = HtmlsinGleform.replace ("singleform_table_id", singleform_table_id);//tableid Assignment htmlsingleform = htmlSingleForm.Replace (  "Post", method);                             Form Submission Method assignment return htmlsingleform;//return} public ActionResult Index () {           Viewbag.form1 = Htmlconvert.convertsingleform ("Singleform", "234", "post");        Viewbag.form1 = Convertsingleform ("Singleform", "234", "post");//load a form control into the ViewBag and upload to the View Return view (); }    }



When the index method in the controller is called, the index view is returned, followed by the code in the index view:


@if (viewbag.form1!= null) {    @Html. Raw (ViewBag.form1.ToString ())  //parsed into Html code displayed on page}

Use the raw HTML method to parse the characters into HTML code.




With this demo, we simply put all the HTML code of the generated control into a string, then use Ctrl+f to find all the quotes, replace it with &quot, and then put the good character into a string or StringBuilder, The substitution of the variable in the method is OK.


In this way, we will process the HTML code of the generated control as a whole without having to disassemble it and then stitch it back together.


It is good to do so, but also left a hidden trouble, this later.




Summary: The process of thinking about countless solutions is more interesting than getting a solution.








How to send a background-encapsulated Web control to the foreground view display in MVC

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.