将简单的 ASP.NET WebForm 转换为 MVC

发布于 2024-12-04 12:26:19 字数 2646 浏览 1 评论 0原文

我有简单的 asp.net 页面:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptTable.DataSource = GetDataFromDB();
            rptTable.DataBind();
        }
    }

    private DataTable GetDataFromDB()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));

        table.Rows.Add(1, "HDD");
        table.Rows.Add(2, "Video");
        table.Rows.Add(3, "RAM");

        return table;
    }

    protected void rptTable_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        TextBox tbName = (TextBox)e.Item.FindControl("tbName");
        int id = Convert.ToInt32(e.CommandArgument);
        string name = tbName.Text;

        //Category.Save(int id, string name)
        //Rebuild Repeater
    }

}

和 aspx 代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
        <asp:Repeater ID="rptTable" runat="server" OnItemCommand="rptTable_ItemCommand">
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("id")%>
                    </td>
                    <td>
                        <asp:TextBox ID="tbName" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:Button ID="btnSave" runat="server" Text="Save" CommandArgument='<%#Eval("id")%>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

它的工作原理类似于简单的网格,您可以直接在网格中编辑数据。

如何在 MVC 中实现相同的逻辑? 谢谢!

I have simple asp.net page:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptTable.DataSource = GetDataFromDB();
            rptTable.DataBind();
        }
    }

    private DataTable GetDataFromDB()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));

        table.Rows.Add(1, "HDD");
        table.Rows.Add(2, "Video");
        table.Rows.Add(3, "RAM");

        return table;
    }

    protected void rptTable_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        TextBox tbName = (TextBox)e.Item.FindControl("tbName");
        int id = Convert.ToInt32(e.CommandArgument);
        string name = tbName.Text;

        //Category.Save(int id, string name)
        //Rebuild Repeater
    }

}

and aspx code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
        <asp:Repeater ID="rptTable" runat="server" OnItemCommand="rptTable_ItemCommand">
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("id")%>
                    </td>
                    <td>
                        <asp:TextBox ID="tbName" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:Button ID="btnSave" runat="server" Text="Save" CommandArgument='<%#Eval("id")%>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

It works like simple grid and you are able to edit data directly in the grid.

How can I implement same logic in MVC ?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-12-11 12:26:19

要在 MVC 应用程序中复制 asp:Repeater 功能,您通常会使用 foreach 循环,其中文本框绑定到模型属性。

但是,如果您确实需要,可以在 MVC 应用程序中包含 asp:repeater 控件,但如果它依赖于回发,则它不会像您期望的那样工作。

如果它是一个简单的只读控件,您可以在视图中放置类似以下内容:

<scipt runat="server">
    protected void Page_Load(object sender, EventArgs e) {
        rptTable.DataSource = ViewData["yourViewDataList"];
        rptTable.DataBind();
    }
</script>

这假设您在 ViewData 中有一些可以进行数据绑定的内容,因此要做到这一点,您需要创建一个控制器方法来构建正如您在“GetDataFromDB()”中所做的那样,

我不会详细介绍完整的实现,但在较高的级别上,这就是您在 MVC 中使用控件的方式。

To replicate the asp:Repeater functionality in an MVC application you would normally use a foreach loop with textboxes within bound to model properties.

You can however if you really want, include an asp:repeater control in an MVC app, but it's not going to work as you'd expect if it relies on postbacks.

If it's a simple read only control, you can put something like the following in your view:

<scipt runat="server">
    protected void Page_Load(object sender, EventArgs e) {
        rptTable.DataSource = ViewData["yourViewDataList"];
        rptTable.DataBind();
    }
</script>

This assumes you've got something in the ViewData which can be databound, so to do that you'd likley need to create a controller method to build your DataTable as you've done in 'GetDataFromDB()'

I'm not going to go into the full implementation but at a high level, that's how you could use the control in MVC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文