2

i have written the following code in my view and i have to print value of variables of my forloop variables company in my view how can i do this

 <table><tr>


        <td>
        <% 
        var ResDet1 = (from r in db.Res_Exp
                           where r.Resume.ResumeID == 191
                           select new{r.ResExpID,r.Company}).ToList();
        string company;
                                int exprinceid;
            if (ResDet1 != null)
            {

                foreach (var rc in ResDet1) 
                {

                    exprinceid = rc.ResExpID;
                    company = rc.Company; 

                }

            }
       %>
     </td></tr></table>
iProgrammer
  • 3,099
  • 3
  • 33
  • 59

4 Answers4

4

i believe following code

var ResDet1 = (from r in db.Res_Exp
                           where r.Resume.ResumeID == 191
                           select new{r.ResExpID,r.Company}).ToList();

belongs to repository and

string company;
int exprinceid;

should be included in your model. Only foreach should appear in the view. In your controller you should call method of your repository and wrap everything in a view model and pass it to the view. In view you should only iterate and render values (either in display mode or in edit mode). Look at this question for how to implement repository pattern in asp.net mvc

Community
  • 1
  • 1
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • 1
    agreed. only thing i'd add is that i would argue not even the `foreach` should be in the view. use a custom display template. – RPM1984 Jun 23 '11 at 07:03
2

Answer - as requested.

Why use a loop? Make use of MVC's templating.

Create a display template called MyModel, where MyModel is whatever type ResDet1 is:

Shared\DisplayTemplates\MyModel.cshtml

<tr>
   <td><% Html.DisplayFor(model => model.ResExpID) %></td>
   <td><% Html.DisplayFor(model => model.Company) %></td>
</tr>

Then your main View simply becomes this:

<table>
   <% Html.DisplayForModel() %>
</table>

How does that work? Well, your View should be strongly-typed to an IEnumerable<ResDet>, which as @Muhammad mention's, should be fetched via a Repository.

Your action should be something like this:

[HttpGet]
public ActionResult Index()
{
   var results = _repository.ResDets();
   return View(results);
}

Ideally, you should be using the Repository pattern to talk to the model, use AutoMapper to flatten the model into a ViewModel, and use interface-driven Dependency Injection to handle the dependencies (e.g _repository).

But if that's too much for you, at the very least, put the data access code in the action method, never, i repeat, never in the View.

Now isn't that nicer?

  1. Controller has 2 lines of code - one to retrieve the model, another to call the View. Textbook stuff.

  2. View is dumb. It renders out what it needs to, and is focused on presentation only.

  3. The foreach loop is avoided, as MVC will do an implicit foreach loop, since it sees the model is of IEnumerable<Something>, so it looks for a template matching that Something, and finds your custom template.

Try not to forget the principles of MVC. If your mixing code and markup in your View, you've already lost and may as well be coding in classic ASP.

RPM1984
  • 72,246
  • 58
  • 225
  • 350
1

Usually you'd do something like:

<% foreach (var someVar in someCollection) 
   { %>
    <%: someVar %>
<% } %>

To print in a loop.

user122211
  • 493
  • 3
  • 6
1

Well, presently, your foreach loop is running entirely within your view engine's code block. The code is running, and those variables are being reassigned a number of times, but you're never telling the C# to write any HTML. I don't write in ASPX, I write in the Razor, so I don't know much for how to escape in and out of C# mode, but I think it'd be something like this:

 <table>
        <% 
        var ResDet1 = (from r in db.Res_Exp
                           where r.Resume.ResumeID == 191
                           select new{r.ResExpID,r.Company}).ToList();
        string company;
        int exprinceid;
            if (ResDet1 != null)
            {
                foreach (var rc in ResDet1) 
                { %>
                <tr>
                    <td><% rc.ResExpID; %></td>
                    <td><% company = rc.Company; %></td>
                </tr>
               <% }

            }
       %>
</table>

EDIT: To be specific, the point is that you want to leave the code to write HTML to the page, and return to finish the code block. As I understand it, MVC is consistent with this principal in both rendering engines.

C. Warren Dale
  • 184
  • 2
  • 11