how to get Model “values” without a foreach over it? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?How do you give a C# Auto-Property a default value?How do you get the index of the current iteration of a foreach loop?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#How to loop through all enum values in C#?How should a model be structured in MVC?How to populate a model without entity framework in Asp.net MVC 5Getting and passing MVC Model data to AngularJS controllerTensorflow: how to save/restore a model?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Can a non-EU citizen traveling with me come with me through the EU passport line?

How to find all the available tools in macOS terminal?

What does the "x" in "x86" represent?

How do I stop a creek from eroding my steep embankment?

Is the address of a local variable a constexpr?

Is there a "higher Segal conjecture"?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Are my PIs rude or am I just being too sensitive?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

How to motivate offshore teams and trust them to deliver?

How do I mention the quality of my school without bragging

Gastric acid as a weapon

What is the correct way to use the pinch test for dehydration?

What is the longest distance a 13th-level monk can jump while attacking on the same turn?

How much radiation do nuclear physics experiments expose researchers to nowadays?

How to bypass password on Windows XP account?

Can Pao de Queijo, and similar foods, be kosher for Passover?

When -s is used with third person singular. What's its use in this context?

Did Xerox really develop the first LAN?

Why was the term "discrete" used in discrete logarithm?

Storing hydrofluoric acid before the invention of plastics

What's the purpose of writing one's academic bio in 3rd person?

Do I really need recursive chmod to restrict access to a folder?



how to get Model “values” without a foreach over it?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?How do you give a C# Auto-Property a default value?How do you get the index of the current iteration of a foreach loop?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Get int value from enum in C#How to loop through all enum values in C#?How should a model be structured in MVC?How to populate a model without entity framework in Asp.net MVC 5Getting and passing MVC Model data to AngularJS controllerTensorflow: how to save/restore a model?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am learning MVC and for do this I am developing a "smart forum".
How can I get value from Model directly , without foreach iteration when data is "a single row"?



@model IEnumerable<WebApplication28.ModelsFromDb.ArgomentiViewModel>
@foreach (var item in Model)


<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.NomeArea)
</dt>

<dd>
@Html.DisplayFor(modelItem => item.NomeArea)
</dd>










share|improve this question

















  • 1





    If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

    – DavidG
    Mar 8 at 16:25

















0















I am learning MVC and for do this I am developing a "smart forum".
How can I get value from Model directly , without foreach iteration when data is "a single row"?



@model IEnumerable<WebApplication28.ModelsFromDb.ArgomentiViewModel>
@foreach (var item in Model)


<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.NomeArea)
</dt>

<dd>
@Html.DisplayFor(modelItem => item.NomeArea)
</dd>










share|improve this question

















  • 1





    If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

    – DavidG
    Mar 8 at 16:25













0












0








0








I am learning MVC and for do this I am developing a "smart forum".
How can I get value from Model directly , without foreach iteration when data is "a single row"?



@model IEnumerable<WebApplication28.ModelsFromDb.ArgomentiViewModel>
@foreach (var item in Model)


<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.NomeArea)
</dt>

<dd>
@Html.DisplayFor(modelItem => item.NomeArea)
</dd>










share|improve this question














I am learning MVC and for do this I am developing a "smart forum".
How can I get value from Model directly , without foreach iteration when data is "a single row"?



@model IEnumerable<WebApplication28.ModelsFromDb.ArgomentiViewModel>
@foreach (var item in Model)


<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.NomeArea)
</dt>

<dd>
@Html.DisplayFor(modelItem => item.NomeArea)
</dd>







c# asp.net-mvc model






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 16:17









surferNetsurferNet

141




141







  • 1





    If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

    – DavidG
    Mar 8 at 16:25












  • 1





    If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

    – DavidG
    Mar 8 at 16:25







1




1





If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

– DavidG
Mar 8 at 16:25





If your data is a single row, then you shouldn't pass in an IEnumerable into the view.

– DavidG
Mar 8 at 16:25












2 Answers
2






active

oldest

votes


















0














That would be messy code if you keep your model type as IEnumerable<>. So I suggest you change that, in one of two ways.



If you know there will always be a single item, why do you need to have a enumerable at all? Just remove it and make sure to return the underlying object from the controller:



@model WebApplication28.ModelsFromDb.ArgomentiViewModel


If you have to deal with collections, I bet it is a more specific type you are returning from the controller. If it is a list, you could use IList<>, which has many more operations, and which you could index:



@model IList<WebApplication28.ModelsFromDb.ArgomentiViewModel>
...
@Html.DisplayNameFor(model => model[0].NomeArea)





share|improve this answer






























    0














    Your ActionMethod is going to have to only return a single row, not an IEnumerable. Use the SingleOrDefault() method on your entity. Return that in your view to ensure that you're only going to at most, return one row.



    @model WebApplication28.ModelsFromDb.ArgomentiViewModel
    <dl class="dl-horizontal">
    <dt>
    @Html.DisplayNameFor(model => model.NomeArea)
    </dt>
    <dd>
    @Html.DisplayFor(modelItem => item.NomeArea)
    </dd>
    </dl>





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067057%2fhow-to-get-model-values-without-a-foreach-over-it%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      That would be messy code if you keep your model type as IEnumerable<>. So I suggest you change that, in one of two ways.



      If you know there will always be a single item, why do you need to have a enumerable at all? Just remove it and make sure to return the underlying object from the controller:



      @model WebApplication28.ModelsFromDb.ArgomentiViewModel


      If you have to deal with collections, I bet it is a more specific type you are returning from the controller. If it is a list, you could use IList<>, which has many more operations, and which you could index:



      @model IList<WebApplication28.ModelsFromDb.ArgomentiViewModel>
      ...
      @Html.DisplayNameFor(model => model[0].NomeArea)





      share|improve this answer



























        0














        That would be messy code if you keep your model type as IEnumerable<>. So I suggest you change that, in one of two ways.



        If you know there will always be a single item, why do you need to have a enumerable at all? Just remove it and make sure to return the underlying object from the controller:



        @model WebApplication28.ModelsFromDb.ArgomentiViewModel


        If you have to deal with collections, I bet it is a more specific type you are returning from the controller. If it is a list, you could use IList<>, which has many more operations, and which you could index:



        @model IList<WebApplication28.ModelsFromDb.ArgomentiViewModel>
        ...
        @Html.DisplayNameFor(model => model[0].NomeArea)





        share|improve this answer

























          0












          0








          0







          That would be messy code if you keep your model type as IEnumerable<>. So I suggest you change that, in one of two ways.



          If you know there will always be a single item, why do you need to have a enumerable at all? Just remove it and make sure to return the underlying object from the controller:



          @model WebApplication28.ModelsFromDb.ArgomentiViewModel


          If you have to deal with collections, I bet it is a more specific type you are returning from the controller. If it is a list, you could use IList<>, which has many more operations, and which you could index:



          @model IList<WebApplication28.ModelsFromDb.ArgomentiViewModel>
          ...
          @Html.DisplayNameFor(model => model[0].NomeArea)





          share|improve this answer













          That would be messy code if you keep your model type as IEnumerable<>. So I suggest you change that, in one of two ways.



          If you know there will always be a single item, why do you need to have a enumerable at all? Just remove it and make sure to return the underlying object from the controller:



          @model WebApplication28.ModelsFromDb.ArgomentiViewModel


          If you have to deal with collections, I bet it is a more specific type you are returning from the controller. If it is a list, you could use IList<>, which has many more operations, and which you could index:



          @model IList<WebApplication28.ModelsFromDb.ArgomentiViewModel>
          ...
          @Html.DisplayNameFor(model => model[0].NomeArea)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 16:24









          AndreiAndrei

          48.3k77093




          48.3k77093























              0














              Your ActionMethod is going to have to only return a single row, not an IEnumerable. Use the SingleOrDefault() method on your entity. Return that in your view to ensure that you're only going to at most, return one row.



              @model WebApplication28.ModelsFromDb.ArgomentiViewModel
              <dl class="dl-horizontal">
              <dt>
              @Html.DisplayNameFor(model => model.NomeArea)
              </dt>
              <dd>
              @Html.DisplayFor(modelItem => item.NomeArea)
              </dd>
              </dl>





              share|improve this answer



























                0














                Your ActionMethod is going to have to only return a single row, not an IEnumerable. Use the SingleOrDefault() method on your entity. Return that in your view to ensure that you're only going to at most, return one row.



                @model WebApplication28.ModelsFromDb.ArgomentiViewModel
                <dl class="dl-horizontal">
                <dt>
                @Html.DisplayNameFor(model => model.NomeArea)
                </dt>
                <dd>
                @Html.DisplayFor(modelItem => item.NomeArea)
                </dd>
                </dl>





                share|improve this answer

























                  0












                  0








                  0







                  Your ActionMethod is going to have to only return a single row, not an IEnumerable. Use the SingleOrDefault() method on your entity. Return that in your view to ensure that you're only going to at most, return one row.



                  @model WebApplication28.ModelsFromDb.ArgomentiViewModel
                  <dl class="dl-horizontal">
                  <dt>
                  @Html.DisplayNameFor(model => model.NomeArea)
                  </dt>
                  <dd>
                  @Html.DisplayFor(modelItem => item.NomeArea)
                  </dd>
                  </dl>





                  share|improve this answer













                  Your ActionMethod is going to have to only return a single row, not an IEnumerable. Use the SingleOrDefault() method on your entity. Return that in your view to ensure that you're only going to at most, return one row.



                  @model WebApplication28.ModelsFromDb.ArgomentiViewModel
                  <dl class="dl-horizontal">
                  <dt>
                  @Html.DisplayNameFor(model => model.NomeArea)
                  </dt>
                  <dd>
                  @Html.DisplayFor(modelItem => item.NomeArea)
                  </dd>
                  </dl>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 9 at 2:46









                  Charles OwenCharles Owen

                  70367




                  70367



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067057%2fhow-to-get-model-values-without-a-foreach-over-it%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко