Using MVC4 DropDownList helper for deeply nested properties - c#

In my view I need to create a drop down list for a property that is not in the immediate view model, rather nested within two more levels of view models. It's arranged as so:
patient -> (list)referrals -> (list)bookings.attendanceId
How would I use the DropDownListFor helper in this case? The problem is not finding the List<SelectListItem> but pointing the helper to the scalar value the drop down selection should fill.

First just an opinion, one of the main reason we use view models if to simplify the domain models to only what you explicitly need in the view. So my question to you is why is your view model so complex?
That being said the only way to accomplish what you want to do is to have your drop down list nested inside 2 for loops like this:
#for (var i = 0; i < Model.Referrals.Count; i++)
{
#for (var j = 0; j < Model.Referrals[i].Bookings.Count; j++)
{
#Html.DropDownListFor(m => m.Referrals[i].Bookings[j].AttendanceId, Model.SomeSelectList)
}
}

Try
#for(int i = 0; i < Model.Referrals .Count; i++)
{
for (int j = 0; j < Model.Referrals[i].Bookings.Count; j++)
{
#Html.DropDownListFor(m => m.Referrals[i].Bookings[j].AttendanceId, Model.YourSelectList)

Related

For loop in model view ASP.NET MVC C#

Can I use a for loop to show the model view list? Or do I have to use foreach?
If yes, can anyone show me an example please? Thank you.
#model IList<TestMVC1.Models.Account>
#{
ViewBag.Title = "Index";
}
<h2>Accounts List</h2>
#{var student = ViewBag.AccoutList; }
#for (int i = 0; i < student.Count; i++)
{
<div>#student[i].CounterID</div>
<div>#student[i].AccountID</div>
<div>#student[i].AccountName</div>
}
#foreach (var item in Model)
{
<div>#item.CounterID</div>
<div>#item.AccountID</div>
<div>#item.AccountName</div>
}
I want to use it with for loop not foreach but it's not working! I mean the model not the viewBag
#for (int i = 0; i < Model.Count ; i++)
{
<div>#model[i].CounterID</div>
<div>#model[i].AccountID</div>
<div>#model[i].AccountName</div>
}
You must use the #model to define the type of model in top of view file.
But if you want to access the data you should use Model or #Model.
Change the #model to #Model
#for (int i = 0; i < Model.Count ; i++)
{
<div>#Model[i].CounterID</div>
<div>#Model[i].AccountID</div>
<div>#Model[i].AccountName</div>
}

MVC Razor For loop binding to ViewModel problem

I am seeing a confusing issue with my viewmodel posting back to my controller and I am confused as of why it is not working. Though I have an idea as of why it may not be working which I explained near the bottom.
Basically I use a for loop to bind my model to HTML in the razor view
#for (int i = 0; i < Model.CheckBoxTag.Count; i++)
{
#if (Model.CheckBoxTag[i].TagTypeName == "test")
{
....
}
}
When I submit the form, the test CheckBoxTag objects are sent to my controller as expected.
However, when I do the same further down the html page only using the Escalation tags:-
#for (int i = 0; i < Model.CheckBoxTag.Count; i++)
{
#if (Model.CheckBoxTag[i].TagTypeName == "test1")
{
...
}
}
The test1 CheckBoxTag objects are not sent back to the controller. (The count is still 3, whereas it should be 6)
The fact it's the same code I am unsure how to tackle it.
My theory: I believe it is not posting back to my controller because the test for loop are the 1st elements in the collection, therefore it always goes into the IF. Whereas the test1 objects are near the bottom of the collection so therefore the IF is skipped quite a few times in the loop.
Is that correct? If not, what could be the issue?
Thanks
As stated in the comments section, the indexers must be consecutive.
Therefore in the for loops I put
#for (int i = 0; i < Model.CheckBoxTag.Count; i++)
{
#if (Model.CheckBoxTag[i].TagTypeName == "test1")
{
// New!
<input type="hidden" name="CheckBoxTag.Index" value="#i" />
...
}
}
So now the indexers "[i]" are now being incremented on every loop

Removing list index while iterating through the list using nested loops

Hey so I'm trying to execute this bit of code, however it is going out of bounds I would assume due to it trying to execute the first loop at the index that was removed. Does anyone know a way I can execute this code without it going out of bounds?
for (int i = myList1.Count - 1; i >= 0; i--)
{
for (int j = 0; j < myList2.Count - 1; j++)
{
if (myList2[j] != myList1[i])
{
myList1.RemoveAt(i);
}
}
}
Obligitory Linq Answer:
myList1 = myList1.Where(i => !myList2.Contains(i)).ToList();
Basically, instead of looping through them both on your own, you use Linq to do it for you. You set myList1 to be the items matching the where clause of [myList2 does not contain item X].

How to pass list of arrays within list of object to the Razor view?

I have a list of arrays which contains Model. I am passing this list to view but not able to access properties of the Model, only getting description written on indexes of arrays.
This is my Controller :
**final1 = list contains model with specific query**
var final22 = new List<Movies>();
var final33 = new Array[acd.Count()];
for (int i = 0; i < acc.Count(); i++)
{
final22 = final1.Where(c => c.Id == acc[i]).ToList();
}
for (int i = 0; i < acd.Count(); i++)
{
final33[i] = final1.Where(c => c.Id == acd[i]).ToArray();
}
var viewmodel = new ShowRecommendationGenreViewModel
{
Show_data = final33.ToList() };
return View("ShowRecommendationGenre", viewmodel);
}
This is my View :
#model Project.Models.ViewModels.ShowRecommendationGenreViewModel
#foreach (var abcc in Model.Show_data)
{
#abcc.GetValue(0)
}
Output i am getting :
Project.Models.Movies
Project.Models.Movies
Kindly help me.
You have two issues:
#abcc.GetValue(0) returns a Movie which you are trying to display. By default, classes do not overload the ToString method, and use the default object implementation, which just displays the type name.
You are using an untyped Array to store your data in Show_data, so you don't know the type of its contents at compile-time. Use a strongly-typed collection instead:
var final33 = new List<List<Movie>>();
....
for (int i = 0; i < acd.Count(); i++)
{
final33.Add(final1.Where(c => c.Id == acd[i]).ToList());
}
Then in your view you can iterate over the movie arrays within the model, accessing the properties of the Movie type:
#model Project.Models.ViewModels.ShowRecommendationGenreViewModel
#foreach (var abcc in Model.Show_data)
{
#abcc[0].MovieTitle
}
as per your code #abcc seems to be list of list of Movies, so type cast it and iterate through that list to get Movies properties.

Model Binding To List Of Objects In ASP.NET MVC with RadioButton

I view list data and I want to edit some field
example as image, list view data, and edit it
i have a field (ex:choosed_value with some value 1,2,3,4,5)
in view, i use radiobutton to get value from choosed_value,
but when i submit to save, i can't get value choosed from radiobutton,
field 'content' saved ok, field 'choosed_value' not save.
my code
in view
view radiobutton
#for (int j = 1; j <= 5; j++)
{
<td>
#Html.RadioButton(Model[i].ID.ToString(), Model[i].choosed_value,Model[i].choosed_value == j) ? true : false )
</td>
}
if choosed_value = 1 then radiobutton with value = 1, checked = true
in controller
[HttpPost]
public ActionResult Index(List<Content> contents)
{
DEMOEntities DbContent = new DEMOEntities();
foreach (Content cont in contents)
{
Content Existed_Cont = DbContent.Contents.Find(cont.ID);
Existed_Cont.Content = cont.Content;
Existed_Cont.choosed_value = cont.choosed_value;
}
DbContent.SaveChanges();
}
please help me ...
I think your radiobutton names are incorrect (first param of Html.RadioButton).
The result input name in browser should not be ID, it should be like [0].choosed_value for correct model binding on POST.
#for (int j = 1; j <= 5; j++)
{
<td>
#Html.RadioButtonFor(x => Model[i].choosed_value, ...)
</td>
}
instead of
#for (int j = 1; j <= 5; j++)
{
<td>
#Html.RadioButton(Model[i].ID.ToString(), Model[i].choosed_value,Model[i].choosed_value == j) ? true : false )
</td>
}
Use the browser dev-tools to see what is actually passed as data to your controller. There should be some fields with the name 'contents' or the model-binder cannot pick them up.
First of all we shouldnt make a database call from action method directly.
Now your ViewModel's list should be of type List<SelectListItem>, in that way you can get the "selected" item's value straight forward.

Categories