将 ViewUserControl 上的属性绑定到 .NET MVC2 中的本地变量

发布于 2024-10-21 17:55:06 字数 244 浏览 1 评论 0原文

我想做这样的事情,其中​​ item 是 .aspx 页面中的本地变量:

<p:ProgressBar  runat="server" Progress="<%#item.Completed/item.Total%>" Width="100" />

绑定表达式未检测本地页面级别变量。有没有办法可以使用 RenderPartial 来完成此任务?

I want to do something like this where item is a local variable in the .aspx page:

<p:ProgressBar  runat="server" Progress="<%#item.Completed/item.Total%>" Width="100" />

the binding expression isn't detecting the local page level variables. Is there a way I can accomplish this wihtout using RenderPartial?

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

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

发布评论

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

评论(2

暮年慕年 2024-10-28 17:55:07

您不应在 ASP.NET MVC 应用程序中使用任何服务器端控件 (runat="server"),因为它们依赖于 ViewState 和 PostBack,而这些概念在 ASP.NET MVC 中已不再存在。唯一的例外是 Webforms 视图引擎使用 面板来实现母版页。也没有绑定的概念。

在 ASP.NET MVC 中,您有一个模型、一个控制器和一个视图。控制器填充一些模型并将其传递给要显示的视图。视图本身可以使用 HTML 助手 生成简单的标记或包含更复杂场景的其他部分视图。

因此,您从定义一个模型开始:

public class MyViewModel
{
    public double Progress { get; set; }
}

然后您有一个控制器将操纵该视图模型:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var completed = 5; // get this from somewhere
        var total = 10; // get this from somewhere
        var model = new MyViewModel
        {
            Progress = (1.0 * completed / total)
        }
        return View(model);
    }
}

最后您将拥有该模型的强类型视图,您将在其中显示标记:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Progress</h2>
    <div><%= Html.DisplayFor(x = x.Progress)</div>
</asp:Content>

You shouldn't use any server side controls (runat="server") in an ASP.NET MVC application because that they rely on ViewState and PostBack which are notions that no longer exist in ASP.NET MVC. The only exception makes <asp:Content> panels used by the webforms view engine to implement master pages. Also there is no notion of binding.

In ASP.NET MVC you have a model, a controller and a view. The controller populates some model and passes it to the view to be shown. The view itself could use HTML helpers to generate simple markup or include other partial views for more complex scenarios.

So you start with defining a model:

public class MyViewModel
{
    public double Progress { get; set; }
}

then you have a controller which will manipulate this view model:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var completed = 5; // get this from somewhere
        var total = 10; // get this from somewhere
        var model = new MyViewModel
        {
            Progress = (1.0 * completed / total)
        }
        return View(model);
    }
}

and finally you would have a strongly typed view to this model where you would show the markup:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" 
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Progress</h2>
    <div><%= Html.DisplayFor(x = x.Progress)</div>
</asp:Content>
等你爱我 2024-10-28 17:55:07

答案是确实没有一个好的方法来做到这一点。您可以使用 ViewDataKey 属性传入模型,但是您必须使用当前 ViewDictionary 中存在的键,因此您将通过此路线向父视图添加一些内容。

如果您想设置仅供查看的属性,可能可以使用自定义 HTML 帮助程序,但最好尝试找到达林提到的解决方法。

The answer to this is that there really is not a good way to do this. You can pass in the model using the ViewDataKey properties, but you're stuck using keys that exist in the current ViewDictionary so you're going to add some cruft to the parent view by going this route.

If you want to set view-only properties, it may be possible using a custom HTML helper but probably just better off trying to find a work around as Darin alludes to.

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