0

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?

1
  • To elaborate on this, I really don't think properties like Width should go in the Model. This is purely View data and is only relevant to the view. It doesn't seem like there is a way with RenderPartial to set properties on the view itself. Commented Mar 13, 2011 at 19:35

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't exactly answer my question. I know I can create a strongly typed control and pass in the model, but I don't want to do it this way. It's extra code and doesn't look very good. I've created an MVC control w/ a code behind rather than attaching a model, and I just want a clean way of populating values to these properties w/out having to pass in a new model object, it isn't needed.
@rhooligan, that's how ASP.NET MVC is supposed to work. If you don't like you still have the alternative classic ASP.NET WebForms where you can use server controls without any problems. I would strongly recommend you going through the basic tutorials here: asp.net/mvc and familiarize with the concept. And to elaborate a little more on your comment about the Width property, it's obviously something that shouldn;t be part of the view model: it should be in your CSS file and then you would assign a class to the div containing the progress bar.
Good call. I did realize that I could solve this scenario with CSS, but there are definitely situations I'd prefer to handle in a View code behind than a ViewModel. That being said, I think the CSS solution is less elegant and readable than what I wanted to do.
@rhooligan, that's probably a reflex you have from WebForms. I was the same when I first started MVC. Now I have completely changed my mind: if it is styling it goes to the CSS, that's what their purpose is. Don't pollute your markup with styling. Don't worry, the more you do MVC, the more you will get accustomed to the pattern and you will start finding things you were considering as nice before, very ugly and vice versa :-)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.