如何在ASP.net网站中实现View Model功能

时间:2022-09-02 08:00:33

I'm working with an asp.NET web site -- something I'm not used to. I have a page with a ton of asp:Textbox controls, all with runat="server", and I have a DataTable full of their values. Basically I want to implement some kind of MVC model-like functionality so I can assign the values to each textbox.

我正在使用asp.NET网站 - 这是我不习惯的。我有一个包含大量asp:Textbox控件的页面,所有这些都使用runat =“server”,并且我有一个DataTable,它们的值已经满了。基本上我想实现某种类似MVC模型的功能,所以我可以为每个文本框分配值。

So, a few questions:

那么,有几个问题:

  1. Is there some way to do this in a fairly best-practice manner? (Dont want to use session, etc)
  2. 有没有办法以相当最好的方式做到这一点? (不要使用会话等)
  3. If not, can anyone suggest a better way to do this?
  4. 如果没有,有人可以建议更好的方法吗?

The general look of one of the textbox is this:

其中一个文本框的一般外观是这样的:

<asp:Textbox runat="server" class="number" name="ns_loanAmt" id="ns_loanAmt" value="???" />

1 个解决方案

#1


1  

The question seem to be quite old and for some reason it has been edited so possibly someone could still be interested.

这个问题似乎很老了,出于某种原因,它已被编辑,所以可能有人仍然感兴趣。

I wonder why the OP hasn't considered the built-int web forms binding. Although nowadays I mostly use MVC, for years we all developed dozens of applications using the ASP.NET data-binding and it surely works.

我想知道为什么OP没有考虑内置的int表单绑定。虽然现在我主要使用MVC,但多年来我们都使用ASP.NET数据绑定开发了许多应用程序,它肯定有用。

In this particular case I would create a class that would stand as a model for a single textbox. The class would contain the value, the CSS class, possibly some other properties.

在这种特殊情况下,我将创建一个类作为单个文本框的模型。该类将包含值,CSS类,可能还包含其他一些属性。

public class RowModel
{
    public string Text { get; set; }
    public string CSSClass { get; set; }
    ...
}

Then I would have a repeater, a gridview or a listview and I would put a textbox in the row template.

然后我会有一个转发器,gridview或listview,我会在行模板中放置一个文本框。

<asp:ListView ID="listView1" runat="server">
  ...
  <ItemTemplate>
     <asp:TextBox id="textBox" runat="server" 
                  cssclass="<%# CSSClass %>" Text="<%# Text %>" /> 
  </ItemTemplate> 

Then, in the code behind, I would just bind the list of model instances to the bindable control.

然后,在后面的代码中,我只是将模型实例列表绑定到可绑定控件。

List<RowModel> rowList;
...
if ( !this.IsPostBack )
{
   listView1.DataSource = rowList;
   listView1.DataBind();
}

or even create an ObjectDataSource and bind declaratively.

甚至创建一个ObjectDataSource并以声明方式绑定。

Then, upon each post back, the list of POSTed values can be read and put back into the database.

然后,在每个回发后,可以读取POSTed值列表并将其放回数据库中。

ASP.NET Web Forms binding always worked and because of the automatic viewstate management specific scenarios were even much simpler than manual, MVC-like state management. For example, insert and edit templates provided in a listview were easily switchable, something that needs some effort in plain MVC (although you have much, much better control over what happens).

ASP.NET Web Forms绑定始终有效,并且由于自动视图状态管理特定方案甚至比手动,类似MVC的状态管理简单得多。例如,列表视图中提供的插入和编辑模板很容易切换,需要在普通MVC中进行一些操作(尽管您可以更好地控制发生的事情)。

#1


1  

The question seem to be quite old and for some reason it has been edited so possibly someone could still be interested.

这个问题似乎很老了,出于某种原因,它已被编辑,所以可能有人仍然感兴趣。

I wonder why the OP hasn't considered the built-int web forms binding. Although nowadays I mostly use MVC, for years we all developed dozens of applications using the ASP.NET data-binding and it surely works.

我想知道为什么OP没有考虑内置的int表单绑定。虽然现在我主要使用MVC,但多年来我们都使用ASP.NET数据绑定开发了许多应用程序,它肯定有用。

In this particular case I would create a class that would stand as a model for a single textbox. The class would contain the value, the CSS class, possibly some other properties.

在这种特殊情况下,我将创建一个类作为单个文本框的模型。该类将包含值,CSS类,可能还包含其他一些属性。

public class RowModel
{
    public string Text { get; set; }
    public string CSSClass { get; set; }
    ...
}

Then I would have a repeater, a gridview or a listview and I would put a textbox in the row template.

然后我会有一个转发器,gridview或listview,我会在行模板中放置一个文本框。

<asp:ListView ID="listView1" runat="server">
  ...
  <ItemTemplate>
     <asp:TextBox id="textBox" runat="server" 
                  cssclass="<%# CSSClass %>" Text="<%# Text %>" /> 
  </ItemTemplate> 

Then, in the code behind, I would just bind the list of model instances to the bindable control.

然后,在后面的代码中,我只是将模型实例列表绑定到可绑定控件。

List<RowModel> rowList;
...
if ( !this.IsPostBack )
{
   listView1.DataSource = rowList;
   listView1.DataBind();
}

or even create an ObjectDataSource and bind declaratively.

甚至创建一个ObjectDataSource并以声明方式绑定。

Then, upon each post back, the list of POSTed values can be read and put back into the database.

然后,在每个回发后,可以读取POSTed值列表并将其放回数据库中。

ASP.NET Web Forms binding always worked and because of the automatic viewstate management specific scenarios were even much simpler than manual, MVC-like state management. For example, insert and edit templates provided in a listview were easily switchable, something that needs some effort in plain MVC (although you have much, much better control over what happens).

ASP.NET Web Forms绑定始终有效,并且由于自动视图状态管理特定方案甚至比手动,类似MVC的状态管理简单得多。例如,列表视图中提供的插入和编辑模板很容易切换,需要在普通MVC中进行一些操作(尽管您可以更好地控制发生的事情)。