1

One thing to note is: I need to get the variable's name, not the property's name.

I have a scenario where I need to pass in either List<int> or List<double> to a view. So in the partial view I bind the model to dynamic:

@model dynamic

var nameOfParameter = Web.Controllers.MemberInfoGetting.GetMemberName(() => Model);

@foreach (var value in list)
{

    <td>@Html.Editor(value, "DoubleTemplate", new { Column =  count, Switcher = (YearOfProgram >= count)})</td>

    sum += (double)value;
    ++count;
}

And this is how I call the partial view from the main view:

 @Html.Partial("_myPartial", Model.CategoryList)

Then I found that I have to know the name(CategoryList) of the list which is passed into the partial view.

Here I found many posts talking about using something like this:

   public static class MemberInfoGetting
    {
        public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
        {
            MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
            return expressionBody.Member.Name;
        }
    }
}

This code doesn't work for me as the result of (nameOfParameter )calling it will always be "Model", rather than "CategoryList".

So, is it possible to achieve what I want?


Update1

The reason why I need to pass in the name of the list is because I need the name of the list to form the name on the html element into something like: CategoryList_1. So Razor knows I am trying to bind the value of the textbox into the 1st element of a property in my view model.

class MyViewModel
{
................
public List<double> CategoryList {get; set;}
................
}

Update2

  @Html.RenderPartial("_VerificationSummarySection",new { ListName = "PracticeEvaluationCreditsVerifiedList", List = Model.PracticeEvaluationCreditsVerifiedList})

I'm now trying to pass in the name of the list by using RenderPartial. But, I cannot find the right way to use it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Franva
  • 6,565
  • 23
  • 79
  • 144
  • Where are you using `GetMemberName` ? How are you calling `GetMemberName` ? this matters; but: lists don't have names; variables *only barely* have names; it all depends on context, hence the need to see how you are using `GetMemberName`. – Marc Gravell Sep 04 '14 at 06:49
  • Hi @MarcGravell yes, you are right. I call the method in my partial view. please check my updated post. thx – Franva Sep 04 '14 at 06:52
  • @pwas I strongly suspect it isn't really a direct duplicate of that; I also strongly suspect that the OP needs to find a different approach – Marc Gravell Sep 04 '14 at 06:52
  • @MarcGravell yes - but in this case the title of question should be changed and the question should be corrected - the first statement is also confusing. –  Sep 04 '14 at 06:54
  • I'm not sure I entirely understand the point of what you're trying to do here. Is this just a "display" view, or will you be binding to a form of some sort? – Tieson T. Sep 04 '14 at 06:56
  • hi @TiesonT. my case has both situations. When condition1 is met, then it should be just a "display" view, otherwise, bind to a form. – Franva Sep 04 '14 at 06:58
  • @Franva As Marc notes, that's going to be pretty hard to do, using `dynamic`. – Tieson T. Sep 04 '14 at 07:04
  • hi @TiesonT. I'm already using dynamic as you can see in my partial view. – Franva Sep 04 '14 at 07:07

1 Answers1

2

Names are relative. The list itself doesn't have a name.

One thing to notice is : I need to get the variable's name, not the property's name.

In your use of the method:

var nameOfParameter = Web.Controllers.MemberInfoGetting.GetMemberName(() => Model);

The name here is Model, and Model is the property; it isn't a variable. You have obtained the name of the property: "Model". You cannot, however, obtain the name as it would have been in a calling context (even in regular C# this is hard; between views, however, it is essentially impossible).

As an aside, with that GetMemberName method, if you did use it to obtain the name of a variable, in IL terms it would actually cease being a variable, and would instead become a field - because that is how "captured variables" are implemented by the compiler.

You should instead make it possible to pass down the name you want; either as additional context on the view, or as a view-model that encapsulates a list and a name, or by creating a "named list" concept (perhaps by subclassing List<T> or similar, or by adding an INamedList interface).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900