I have a partial view in mvc3 for rendering tbody content.
So how can I pass count vale in partial view and get counter value in my Serial Number column
PartialViews can have Models assigned to them just like regular views:
#model Namespace.SubNamespace.Folder.ClassName
Then, you can call Html.Partial and pass a new instance of that model:
#Html.Partial("ViewName", new ClassName() {//set values here});
If you do this, you can calculate the number of rows before the partial call and use that elsewhere:
#{
var numRows = dataSource.Rows.Count(); //use whatever count method you like
}
Meaning you can calculate the row count and set it as part of the PartialView's view model:
#{
var numRows = dataSource.Rows.Count(); //use whatever count method you like
}
#Html.Partial("ViewName", new ClassName() {RowCount = numRows});
Related
(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)
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.
Debugging I get this:
So, User_SportsList is a MultiSelectList with the proper Items and the proper SelectedValues.
I pass to the view like this:
ViewBag.usl = User_SportsList;
In my View I have this:
#Html.ListBox("UserSports", ViewBag.usl as MultiSelectList)
And is showing the list with all the items, but SelectedValues aren't working. What I'm doing wrong ?
The SelectedValues property needs to contain an array of values (where "value" is defined by DataValueField), and not the items themselves. So, in this example, you should set SelectedValues equal to:
User_SportsList.SelectedValues = new [] { 3, 4 };
I want to pass two values from controller action to asp.net MVC 3 Razor view. I am doing like this in action method:
var model = new { reportid = rid, refno = refernumber};
return View(model );
but when i try to access it in view like this:
#Model.reportid
I get, object doesn't contain property reportid
How can I pass multiple values without using viewbag ?
Well, i strongly recommend you to use ViewModel class. But if for some reason you have phobia of extra viewmodel classes, you can use C# feature called Tuple
var model = Tuple.Create(firstObject, secondObject)
and then your view will be of type Tuple, for example
#model Tuple<int, string>
and you can access them like this
#Model.Item1
And then you can congratulate yourself, you have achieved nice mess with hidden meaning :)
Another way to accomplish this task - is to use ExpandoObject.
dynamic model = new System.Dynamic.ExpandoObject();
model.reportid = 123
model.refno = 456;
Set the view model type to dynamic:
#model dynamic
#Model.reportid
#Model.refno
I have a service function to query a SQL db like so:
public IQueryable<MyModel> getAll()
{
IQueryable<MyModel> models = (from f in db.MyModel select f);
return models;
}
When I implement it in my controller it works when I chain the Take():
var models = myModelEntities.getAll().Take(5);
return View(models); // returns 5 rows to the view
But like this it doesn't:
var models = myModelEntities.getAll();
models.Take(5);
return View(models); // returns thousands of rows to the view
Why is Take() ignored if it's not chained? I have Lazy Loading enabled on my model...
It does work, but you're not assigning the result of Take() to anything. Take() doesn't mutate the models variable, it returns the top 5 items in the enumerable.
The following would work:
var models = myModelEntities.getAll();
models = models.Take(5);
return View(models); // returns five rows to the view
It's because Take doesn't mutate the current IEnumerable; it returns a new one. This will work:
models = models.Take(5);
You aren't assigning the result of the second Take() call to any variable.