将ViewModel属性绑定到ASP TextBox和Button

时间:2013-09-30 12:49:30

标签: asp.net c#-4.0 mvvm objectdatasource webmethod

这是我的观点

<%@ 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>

我希望从我的viewmodel绑定属性和方法。可能吗?

这是我的ViewModel

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。

这是我的示例从ViewModel与CRUD操作绑定到我的带网格视图

<%@ 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>

UserListVM

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();
        }
    }
}

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

0 个答案:

没有答案