Does ASP.NET MVC use the regular toolbox controls?

后端 未结 7 2120
予麋鹿
予麋鹿 2020-12-15 01:55

In ASP.NET MVC, do I use the \"regular\" controls from the toolbox and assign data to them like I did with webforms? Can I use the gridview, for example?

Thank you.<

7条回答
  •  旧时难觅i
    2020-12-15 02:21

    You cannot use WebForms Controls in ASP.NET MVC directly.
    But you can wrap any control inside an HTML helper.

    Example a GridView:

    public static class MvcGrid
        {
            public static string MyGrid(this HtmlHelper helper)
            {
                var grid = new GridView();
                var source = new[]
                                 {
                                     "Foo",
                                     "Bar",
                                 };
                grid.DataSource = source;
                grid.DataBind();
                var stringWriter = new StringWriter();
                var writer = new HtmlTextWriter(stringWriter);
                grid.RenderControl(writer);
    
                return stringWriter.ToString();
            }
        }
    

    But this will not function as a WebForm GridView.
    It is just for rendering the HTML.

提交回复
热议问题