简体   繁体   English

将ViewUserControl的属性绑定到.NET MVC2中的localvariable

[英]Bind property on ViewUserControl to localvariable in .NET MVC2

I want to do something like this where item is a local variable in the .aspx page: 我想做这样的事情,其中​​item是.aspx页中的局部变量:

<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? 有没有一种方法,我可以做到这一点wihtout使用的RenderPartial?

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. 您不应在ASP.NET MVC应用程序中使用任何服务器端控件( runat="server" ),因为它们依赖于ViewState和PostBack,这是ASP.NET MVC中不再存在的概念。 The only exception makes <asp:Content> panels used by the webforms view engine to implement master pages. 唯一的例外是webforms视图引擎使用<asp:Content>面板来实现母版页。 Also there is no notion of binding. 也没有绑定的概念。

In ASP.NET MVC you have a model, a controller and a view. 在ASP.NET MVC中,您有一个模型,一个控制器和一个视图。 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. 该视图本身可以使用HTML帮助器来生成简单的标记,也可以包含其他部分视图以用于更复杂的场景。

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>

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. 您可以使用ViewDataKey属性来传递模型,但是您将无法使用当前ViewDictionary中存在的键,因此您将通过此路线将一些内容添加到父视图中。

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. 如果要设置仅查看属性,则可以使用自定义HTML帮助程序,但最好尝试找到Darin所暗示的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM