Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list data on view in controller

Tags:

People also ask

How do you pass list of values from controller view?

ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.

How do you get data from models to view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How do you pass list data from Controller view using ViewBag?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.


I have a view in which I have rendered partial view in a loop. There is a list and partial view have is binded with each item in a list. I am not getting the value of the list on controller after value is entered.

Here is my view:

<table id="resourceRequirement" class="table" width="100%" border="0">
    <thead>
        <tr style="background-color:#dfdfdf;">
            <td><div align="center">PRIORITY</div></td>
            <td><div align="center">SYSTEM RESOURCE / COMPONENT</div></td>
            <td><div align="center">RECOVERY TIME OBJECTIVE</div></td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.ResourceRequirement)
        {
            @Html.Partial("~/Views/Shared/_ResourceRequirement.cshtml", item)
        }
    </tbody>
</table>

Here is my partial view:

@model DisasterManagementSystem.Models.BusinessImpactAnalysis.ResourceRequirement
<tr>
    <td>
        @Html.TextBoxFor(m => m.priority)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.priority)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.systemresource)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.systemresource)
        </div>
    </td>
    <td>
        @Html.TextBoxFor(m => m.receveryTime)<br />
        <div style="color:red;">
            @Html.ValidationMessageFor(model => model.receveryTime)
        </div>
    </td>
</tr>

Here is my list:

public List<ResourceRequirement> ResourceRequirement { get; set; }

And the class is here:

public class ResourceRequirement
{
    [Required(ErrorMessage = "*")]
    public string priority { get; set; }

    [Required(ErrorMessage = "*")]
    public string systemresource { get; set; }

    [Required(ErrorMessage = "*")]
    public string receveryTime { get; set; }
}

Please advise when i am trying to get the list from model on post I am getting the list as null.