3

这是我的观点

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InsertUser.aspx.cs" Inherits="WebApplication1.InsertUser" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"></asp:ObjectDataSource>

        <asp:TextBox></asp:TextBox> // i want to bind the Fullname Property here
        <asp:Button></asp:Button> // i want to bind the Save() here
    </div>
    </form>
</body>
</html>

我想从我的视图模型中绑定属性和方法。可能吗?

这是我的视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services;

namespace Testing.ViewModel
{
    public class UserVM
    {
        public int UserID { get; set; }
        public string Fullname { get; set; }

        [WebMethod]
        public void Save()
        {

        }
    }
}

有人可以引导我走向正确的方向吗?我设法将 Grid 与 ObjectDataSource 绑定,并且我可以执行 CRUD 操作,但我无法设法将 View 绑定到 ViewModel。

这是我从带有 CRUD 操作的 ViewModel 到带有 Grid 的视图的示例绑定

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body><form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="Testing.Model.User" DeleteMethod="DeleteUser" InsertMethod="CreateUser" SelectMethod="GetUsers" TypeName="Testing.ViewModel.UserListVM" UpdateMethod="UpdateUser"></asp:ObjectDataSource>
        <asp:GridView ID="GridView1" DataSourceID="ObjectDataSource1" runat="server" AllowPaging="True" AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
                <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
                <asp:BoundField DataField="Fullname" HeaderText="Fullname" SortExpression="Fullname" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

用户列表虚拟机

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Testing.Model;
namespace Testing.ViewModel
{
    public class UserListVM
    {
        public UserListVM()
        {
        }
        public IEnumerable<User> GetUsers()
        {
            return VMIndex.Users;
        }
        public User UpdateUser(User user)
        {
            throw new NotImplementedException();
        }
        public User DeleteUser(User user)
        {
            throw new NotImplementedException();
        }
        public User CreateUser(User user)
        {
            throw new NotImplementedException();
        }
    }
}

请提供任何帮助..提前致谢。

4

0 回答 0