MVC 2 - Display first result only in for each loop - asp.net-mvc

I've got what appears to be a fairly basic loop:
<% foreach (var item in Model.Items.OrderByDescending(b => b.ItemDateTime)) {%>
Instead of looping through all the items I just want to output the first item, how can I do this?

You can use FirstODefault() method of your collection. Try something like this:
// check if the model contains any item
if (Model.Items.Count() > 0)
{
//show the item...
var firstItem = Model.Items.OrderByDescending(b => b.ItemDateTime).FirstOrDefault();
}

To expand on Felipe's comment, it's better design usually to put things like this in your view models or controllers, not the views.
You could put this on your viewmodel
public Item EarliestItem
{
get { return Items.OrderByDescending(b => b.ItemDateTime).FirstOrDefault(); }
}
Then use this in your view
Or whatever it is you want to do with the earliest item.

Related

How to loop thru view model for particular value using foreach and where

(NUBE) I am trying to loop thru a view model and show rows when a field has a particular value. (IE In a Master/Detail model I have pulled all my orders in one list and all the detail in another. When the user picks the particular order from the first list I just want to iterate thru the detail list and show those line items for edit/delete purposes.
This code works from testing if I hardcode the value (2953) but I wish to pass a value and am not sure if Viewbag or razor is the way to go. I would like to maintain a consistent framework throughout.
#foreach (var row in Model.SplitTankAssignmentItems.Where(x => x.MeterValueID == 2953) ) // row.splitTicketID) )
{
<tr id="#row.SplitTankID>
:
:
:
</tr>
}
The ViewBag is exactly for this purpose.
On your control-action method you can set up the ViewBag and in your view you can read it.
Controller
public IActionResult Index()
{
:
ViewBag.ValueToUse = 2953
Return View(model);
}
View
#foreach (var row in Model.SplitTankAssignmentItems.Where(x => x.MeterValueID == (int)ViewBag.ValueToUse) ) // row.splitTicketID) )
{
<tr id="#row.SplitTankID>
:
:
:
</tr>
}
Notes: Remeber to cast your ViewBag property to the type you need, and keep in mind that ViewBag is only for one call, usually to take information from controller to view. It won't stay from view to controller neither from an action method to action method (RedirectTo)

Aspx to Razor Select List MVC5

I have converted my MVC3 application to MVC5, I had to change all views to razor. Having a challenge with a select list:
In ASPX view that works I am using the following:
<select id="Profession" name="Profession" style="width: 235px; background-color: #FFFFCC;">
<% List<string> allProfessions = ViewBag.AllProfessions;
string selectedProfession;
if (Model != null && !String.IsNullOrEmpty(Model.Profession))
selectedProfession = Model.Profession;
else
selectedProfession = allProfessions[0];
foreach (var aProfession in allProfessions)
{
string selectedTextMark = aProfession == selectedProfession ? " selected=\"selected\"" : String.Empty;
Response.Write(string.Format("<option value=\"{0}\" {1}>{2}</option>", aProfession, selectedTextMark, aProfession));
}%>
</select>
In Razor I am using:
<select id="Profession" name="Profession" style="width: 235px; background-color: #FFFFCC;">
#{List<string> allProfessions = ViewBag.AllProfessions;
string selectedProfession;}
#{if (Model != null && !String.IsNullOrEmpty(Model.Profession))
{selectedProfession = Model.Profession;}
else {selectedProfession = allProfessions[0];}
}
#foreach (var aProfession in allProfessions)
{
string selectedTextMark = aProfession == selectedProfession ?
"selected=\"selected\"" : String.Empty;
Response.Write(string.Format("<option value=\"{0}\" {1}>{2}</option>",
aProfession, selectedTextMark, aProfession));
}
</select>
The list shows up at the top of the page, I can't figure out where is the problem. Would appreciate your assistance.
Don't create your dropdown manually like that. Just use:
#Html.DropDownListFor(m => m.Profession, ViewBag.AllProfessions, new { style = "..." })
UPDATE
I tried your solution but got this error: Extension method cannot by dynamically dispatched
And, that's why I despise ViewBag. I apologize, as my answer was a little generic. Html.DropDownList requires the list of options parameter to be an IEnumerable<SelectListItem>. Since ViewBag is a dynamic, the types of its members cannot be ascertained, so you must cast explicitly:
(IEnumerable<SelectListItem>)ViewBag.AllProfessions
However, your AllProfessions is a simple array, so that cast won't work when the value gets inserted at run-time, but that can be easily fixed by casting it to a List<string> and then converting the items with a Select:
((List<string>)ViewBag.AllProfessions).Select(m => new SelectListItem { Value = m, Text = m })
There again, you see why dynamics are not that great, as that syntax is rather awful. The way you should be handling this type of stuff is to use your model or, preferably, view model to do what it should do: hold domain logic. Add a property to hold your list of profession choices:
public IEnumerable<SelectListItem> ProfessionChoices { get; set; }
And then, in your controller action, populate this list before rendering the view:
var model = new YourViewModel();
...
model.ProfessionChoices = repository.GetAllProfessions().Select(m => new SelectListItem { Value = m.Name, Text = m.Name });
return View(model);
repository.GetAllProfessions() is shorthand for whatever you're using as the source of your list of professions, and the Name property is shorthand for how you get at the text value of the profession: you'll need to change that appropriately to match your scenario.
Then in your view, you just need to do:
#Html.DropDownListFor(m => m.Profession, Model.ProfessionChoices)
Given that you don't have this infrastructure already set up, it may seem like a lot to do just for a drop down list, and that's a reasonable thing to think. However, working in this way will keep your view lean, make maintenance tons easier, and best of all, keep everything strongly-typed so that if there's an issue, you find out at compile-time instead of run-time.
I believe it's happening because of the Response.Write. Try this:
#Html.Raw(string.Format("<option value=\"{0}\" {1}>{2}</option>", aProfession,
selectedTextMark, aProfession))

Is it possible to dynamically set selectedvalue using dropdownlistfor with nested classes?

I have a class, Farm, which contains a list of classes of type Animal.
public class Farm {
public list<Animal> Animals;
// other members..
}
public class Animal {
public string Name;
public string Family;
}
I would like to make a createOrEdit view for my Farm object, and I'd like to use DropDownLists for the Animal's Name and Family. The choices are coming from a database.
When I pass the Animal model to the view for editing I'd like to have the DropDownLists somehow match the properties for each animal and set the selected values of the lists.
I've tried lots of things like this:
#for(int i = 0; i < Model.Animals.Count; i++)
{
#Html.DropDownListFor(model => model.Animals[i].Name, Model.AnimalNames)
// where Model.AnimalNames is a SelectList
#Model.Animals[i].Name // (for testing) this properly displays the name I want to be selected in the list
}
I've seen a bunch of suggestions on this site for creating SelectLists in the controller, iterating through each item and setting the selected property where appropriate. But there's gotta be a cleaner way.. what if I have 100 Animals on my farm. It doesn't seem reasonable to create 200 SelectLists in the controller, iterate through each of them to match up the selected values, and then pass that to the view.
So, is there a simple way for me to take that Animal.Name[i] value and find its matching listitem in the DDL?
Thanks!!
Inside your foreach loop you can do:
#for(int i = 0; i < Model.Animals.Count; i++)
{
var original = Model.AnimalNames;
var animalSelected = new SelectList(original.Items, original.DataValueField, original.DataTextField, model.Animals[i].Name);
#Html.DropDownListFor(model => model.Animals[i].Name, animalSelected)
}
This way, in your controller you create just one SelectList with the AnimalNames... and in your view, you create SelectList with the selected value for each one.
We use this approach when using list of editable items with the need of display the current value of each item.

Sort Generic List to show using foreach (mvc)

I've a List<> included in ViewData.
reloadCollectionModel.ReloadCollection = new List<ReloadModels>();
foreach (DatoPaquete dato in result)
{
reloadModel = new ReloadModels();
reloadModel.IdPaquete = dato.id;
reloadModel.Value = dato.monto;
reloadCollectionModel.ReloadCollection.Add(reloadModel);
}
//disordered (it's works)
ViewData["ReloadModelsCollection"] = reloadCollectionModel.ReloadCollection.AsEnumerable();
Then in razor I show this List using foreach. This works.
#foreach (var item in ViewData["ReloadModelsCollection3"] as List<ReloadModels>)
{
}
My problem... When I sort the list, I can't use foreach to show them (launch Exception).
ViewData["ReloadModelsCollection3"] = reloadCollectionModel.ReloadCollection.OrderBy(item => item.Value).AsEnumerable();
I need to change my ViewData? Or change my foreach?
Thanks in advance, sorry for my bad english.
Because you are casting to a List. After ordering make them a list as follows:
ViewData["ReloadModelsCollection3"] =
reloadCollectionModel.ReloadCollection.OrderBy(item => item.Value).ToList();
That is not AsEnumerable

LINQ to Entites: sub-objects disappearing

I'm trying to understand why, sometimes, my sub-objects disappear.
In my List view, I have the standard
<% foreach (var item in Model)
and when I inspect the item and the model, I can see that item.Map has a couple of elements.
In fact, in my List view I can do:
<% foreach (var map in item.Map)
<% Html.RenderPartial("MapView", map); %>
and MapView can access and display all the properties. I'm including the sub-objects with:
list = from item in _entities.DataTable
.Include("LookupTable1")
.Include("Map")
.Include("Map.LookupTable2") select item;
return View("List", list);
But if I try to do:
<%= Html.Encode(item.Map.FirstOrDefault().Field)%>
I get a null pointer exception, and item.Map has 0 elements.
Can anyone explain why this is happening?
You could probably do without the .Include statements. I'm not sure if that solves your problem though, without knowing the cause of the null-pointer.
Just as a note: when doing MVC, you probably shouldn't be doing Linq queries in the view (like .FirstOrDefault).
list = from item in _entities.DataTable
select new
{
Name = item.Name
Maps = item.Maps
};
Using this syntax you can execute more Linq queries in the controller instead
list = from item in _entities.DataTable
select new
{
Name = item.Name
FirstMap = item.Maps.FirstOrDefault()
};
Or even
list = from item in _entities.DataTable
select new
{
Name = item.Name
Maps = from map in item.Maps
where map = somecondition
select map
};

Resources