0

Previous post

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<%
    MvcApplication1.Models.FeaturesRepository _model = new MvcApplication1.Models.FeaturesRepository();
%>

<% foreach (var md in _model.GetAllFeatures())
   { %>
       <li><%= md.vcr_FeaturesName  %></li> 
<% } %>

It is with reference to the previous post above.Is there something wrong with the foreach loop(The result is correct but it is displaying the series of Add,Add,Add,Add,Add,Add...,which is the last record of the getallfeatures.

4
  • 3
    You are violating everything the mvc pattern stands for with this code. Commented May 10, 2010 at 20:45
  • 1
    why would you post the commented code as well, please simplify your example, and instead of double posting, write a single comprehensive question. And as for the MVC patterns that Mattias talks about. read this blog post : howmvcworks.net/OnViews/BuildingAStronglyTypedView that should help a bit for this specific case... Commented May 10, 2010 at 21:10
  • I'm no MVC expert and this might not be the heart of what's wrong, but I would modify the control so that you pass it your collection as a parameter. I think the sin Mattias is describing is that you're both declaring a Model variable AND retrieving data from the database in this control and you shouldn't be doing either of these here. Commented May 10, 2010 at 21:10
  • Removed the commented out code Commented May 10, 2010 at 22:54

1 Answer 1

-1

@mazhar, instead of creating your model like you are, in MVC you should return the model to the view.

In your controller you would say something like return View(MyModel);

I don't see anything wrong, per-sey, with your foreach but if you are going to replicate a control over and over you may want to consider a PartialView and rendering that by passing the appropriate model to it.

<% foreach ( var md in model.features ) Html.RenderPartial(md); %>

The above is untested but close I think.

I haven't looked at the previous post because I think you need to get this into the MVC way first. I don't think there is technically anything incorrect in your code and suspect it's your controller and model code.

I've edited your post to remove the commented out code. Very confusing leaving it in.

Sign up to request clarification or add additional context in comments.

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.