0

Hello everyone I'm trying to return an array from controller to view. Here is my controller:

public ActionResult Index()

        {
            TahakkukServicesClient client = new TahakkukServicesClient();
            client.ClientCredentials.UserName.UserName = "service_test";
            client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            MakbuzList[] liste = client.GetMakbuzListe(2);
            return View(liste);
        }

    }
}

So how can I display my array's values in my view?

2

2 Answers 2

1

MakbuzList[] "is" an IEnumerable<MakbuzList> (Note : nowadays, Generic lists / collections are usually preferred to arrays)

So you can strongly type your view like that

@model IEnumerable<MakbuzList>

and display values like this

@if (Model.Any()) {
  foreach (var makbuz in Model) {
     @Html.DisplayFor(x => makbuz.Property1)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

IIRC this will not work because the expression must always resolve the object to display starting from the model itself. I 'm not 100% sure though.
@Jon It won't work well with editorFor and binding (where you need a for loop), but I can tell you it's gonna be fine for DisplayFor.
0

Assuming the view is already configured with

@model IList<MakbuzList>

Then you can simply do

@for (var i = 0; i < Model.Count; ++i) {
    @Html.DisplayFor(m => m[i]);
}

1 Comment

It does not work I get this error no extension method "Length"

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.