codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

How to pass an IEnumerable or queryable list of properties from Controller to View


By : , Category : asp.net-mvc

I JUST FOUND A SOLUTION TO MY PROBLEM :'D DAYUMN, this issue was buggin me O.Ô

Just in case some future noob will have the same problem I had:

in my Controller I have this:

 Dim categoryList As New List(Of IEnumerable)
    For Each DistinctCategory In (From prod In database.tblProducts Select prod.prodcategor< Distinct)
        categoryList .Add(DistinctCategor)
    Next

    ViewBag.allCategoriesList = categoryList 

While in my View I have:

<% For Each cat In ViewBag.allCategoriesList %>
                        <a href="/Index/?category=<%= cat%>"> <%=cat%></a>
                    <%Next%>

Now I can select this Link from my View to pass back the filter for category and only select the products that are in this particular category. I just couldn't figure out how to get these unique values out of my ViewModel, thus, I would be really happy to see any suggestions not using ViewBag (Y)

happy

ReLated :

The problem is in fact that this expression:

@Html.HiddenFor(modelItem => item.ID_agent)

and similar as well cannot derive a correct name for the HTML input control, and the resulting request parameters are not parsed by model binder. Usually this is fixed by replacing foreach with for:

@fore (int i=0; i<Model.Count; i++)
{
   @Html.HiddenFor(modelItem => modelItem[i].ID_agent)
   @Html.RadioButtonFor(modelItem => modelItem[i].SelectedAgent, modelItem[i].ID_agent)           

   @Html.DisplayFor(modelItem => modelItem[i].nom_agent) 
   @Html.DisplayFor(modelItem => modelItem[i].prenom_agent)           
    <br />           

}

Note that you would need your view to be typed with IList<> or an array to allow this behavior.

I think it looks like you should be using an Ajax action link. This is available in Razor so you can run it client side like your code is here and it will load the objects you need.

https://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.actionlink(v=vs.118).aspx

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/

If you need to run a partial postback, you could also use an Ajax form in Razor.

http://www.codeproject.com/Articles/429164/Html-BeginForm-vs-Ajax-BeginForm-in-MVC

Hope that helps

Use a list instead and replace your foreach loop with a for loop:

@model IList<BlockedIPViewModel>

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken()

    @for (var i = 0; i < Model.Count; i++) 
    {
        <tr>
            <td>
                @Html.HiddenFor(x => x[i].IP)           
                @Html.CheckBoxFor(x => x[i].Checked)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].IP)
            </td>
        </tr>
    }
    <div>
        <input type="submit" value="Unblock IPs" />
    </div>
}

Alternatively you could use an editor template:

@model IEnumerable<BlockedIPViewModel>

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken()
    @Html.EditorForModel()   
    <div>
        <input type="submit" value="Unblock IPs" />
    </div>
}

and then define the template ~/Views/Shared/EditorTemplates/BlockedIPViewModel.cshtml which will automatically be rendered for each element of the collection:

@model BlockedIPViewModel
<tr>
    <td>
        @Html.HiddenFor(x => x.IP)
        @Html.CheckBoxFor(x => x.Checked)
    </td>
    <td>
        @Html.DisplayFor(x => x.IP)
    </td>
</tr>

The reason you were getting null in your controller is because you didn't respect the naming convention for your input fields that the default model binder expects to successfully bind to a list. I invite you to read the following article.

Once you have read it, look at the generated HTML (and more specifically the names of the input fields) with my example and yours. Then compare and you will understand why yours doesn't work.

Have you tried:

<select>
    @foreach (var contact in ViewBag.ListeContacts)
         {
           <option>@Html.DisplayName(contact["nom_contact"].ToString())</option>
         }
</select>

It looks like you have a list of Contact and you are trying to get a DataRow from the collection?

EDIT: Also make sure the property you are referencing exists, i.e. nom_contact

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .