ASP.NET MVC页面,其中包含多个用户控件实例,这些实例使用独立数据

时间:2022-06-25 09:26:14

First let me thank all of you for numerous useful posts I have read during my recent ASP.NET MVC development. My questions is as follows:

首先,我要感谢大家在我最近的ASP.NET MVC开发过程中阅读的许多有用的帖子。我的问题如下:

I have a ASP.NET MVC application with loosely coupled model, meaning I use a business layer assembly in a controller to access the data and return data to a view by setting Viewdata["MyData"] from the controller.

我有一个松散耦合模型的ASP.NET MVC应用程序,这意味着我在控制器中使用业务层程序集来访问数据并通过从控制器设置Viewdata [“MyData”]将数据返回到视图。

Now my question is how to set individual data of a user control which has multiple instances in a MVC page.

现在我的问题是如何设置在MVC页面中具有多个实例的用户控件的单个数据。

Eg. I have a text box inside userctl.ascx as:

例如。我在userctl.ascx中有一个文本框:

<%= Html.TextBox("ApprovalDate", ViewData["ApprovalDate"],
                 new { @ReadOnly = "True" })%>

If i set ViewData["ApprovalDate"] to be something, how does it apply to individual control data?

如果我将ViewData [“ApprovalDate”]设置为某种东西,它如何应用于单个控制数据?

Appreciate your help.

感谢你的帮助。

1 个解决方案

#1


1  

In your case it would work if you set your user controls (partial views in MVC terminology) to be strongly typed.

在您的情况下,如果您将用户控件(MVC术语中的部分视图)设置为强类型,它将起作用。

Then you can initialize the model and pass it to each individual partial view without those values getting mixed up.

然后,您可以初始化模型并将其传递给每个单独的局部视图,而这些值不会混淆。

Of course, you need to pass to the view the data for all hosted controls in some form.

当然,您需要以某种形式将所有托管控件的数据传递给视图。

#1


1  

In your case it would work if you set your user controls (partial views in MVC terminology) to be strongly typed.

在您的情况下,如果您将用户控件(MVC术语中的部分视图)设置为强类型,它将起作用。

Then you can initialize the model and pass it to each individual partial view without those values getting mixed up.

然后,您可以初始化模型并将其传递给每个单独的局部视图,而这些值不会混淆。

Of course, you need to pass to the view the data for all hosted controls in some form.

当然,您需要以某种形式将所有托管控件的数据传递给视图。