goscamp
goscamp

Reputation: 1106

Add model into model using Html helper HiddenFor C# MVC

I have a model like

public class Model
{
    public int Value { get; set; }
    public List<OtherModel> List { get; set; }
}

public class OtherModel
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }
    public bool IsPropTrue { get; set; }
}

I am using Model in a View where I'm looping through the List to show data in a table.

Depending on whether one of the properties (IsPropTrue) in OtherModel is true or false, I want to use the HiddenFor Html helper and send the data to the HttpPost controller.

@model Model                                       
@foreach (var item in Model.List)
{                                          
    if (item.IsPropTrue)
    {
        @Html.HiddenFor(model=> item.Value1)
        @Html.HiddenFor(model=> item.Value2) 
    } 
}                          

I think it doesn't work because I should in some way add these properties to the OtherModel, which is inside the Model; But the way I have it now, I am adding properties to Model.

Upvotes: 3

Views: 315

Answers (3)

user1489673
user1489673

Reputation:

Consider if it the responsibility of the view or the controller action to make the decisions - you can send everything back to the action to do the decision making.

In your Views/Shared folder, create a controller called EditorTemplates In this folder, add a partial view called OtherModel In this view, set the model to OtherModel and set the Layout=null Add the three OtherModel fields in EditorFor (and HiddenFor if not displaying isPropTrue). This partial view displays just one instance of your list.

In your main view, use the above editor model like so. MVC will take care of all rendering and postback of the Model State for your complete list of items. We like one-liners...

@Html.EditorFor(model => model.OtherModel)

When the data is subsequently posted back to an action, Model State has wrapped up all of your displayed items into a list again, so you can check the isPropTrue value for each item on the server.

The only issue with MVC is that is you pass an empty list out to a view, you get a null value back, so just replace this with an empty list when null is returned

Upvotes: 0

Mehrdad
Mehrdad

Reputation: 1731

if you want send an array to server based on the Model you have to use indexer in @Html.HiddenFor .

@model WebApplication1.Models.MyModel

<form>
    @if (Model != null && Model.List != null)
    {
        for (int i = 0; i < Model.List.Count; i++)
        {
            if (Model.List[i].IsPropTrue)
            {
                @Html.HiddenFor(model => Model.List[i].Value1)
                @Html.HiddenFor(model => Model.List[i].Value2)
            }
        }
    }
    <button type="submit">submit</button>
</form>

if you want know reason of using indexer on model i recommend How does MVC 4 List Model Binding work?

Upvotes: 0

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3403

you can do it like this :

@model Model                                       
@foreach (var item in Model.List)
{                                          
    if (item.IsPropTrue)
    {
        @Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value1)
        @Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value2)
    } 
} 

this way the binding system will bind the hidden fields with your List OtherModel in the Model

Upvotes: 1

Related Questions