Search code examples
c#asp.net-coreasp.net-core-mvc

ASP.NET Core MVC : unable to get the model values after submitting the form


I am trying to create a form with a collection of filters which is a text box content to get the input from users. when submitting I am unable to see my model information is always submitted as null to the controller.

If I use IFormCollection, then I can see values for each of the text boxes. After searching different posts I tried by creating another model class by wrapping the FilterCondition as collection property but not helped me. Any help will really appreciated

My cshtml is:

<table>
@{
    for (int i = 0; i < 3; i++)
    {
        <tr>
            <td>
                @Html.TextBox(i+".Code")
            </td>
            <td>
                @Html.Hidden(i+".FieldName")
            </td>
            <td>
                @Html.TextBox(i+".FieldValue")
            </td>
            <td>
                @Html.TextBox(i+".Operator")
            </td>
        </tr>
    }
    <tr>
        <td>
            <input type="submit" value="Create" />
        </td>
    </tr>
}
</table>

My model class:

public class FilterCondition
{
    public string? Code { get; set; }
    public string? FieldName { get; set; }
    public string? FieldValue { get; set; }
    public string? Operator { get; set; }
}

My controller:

[HttpPost]
public ActionResult Submitter(IEnumerable<FilterCondition> formVals)
{
    try
    {
        return RedirectToAction(nameof(Index));
    }
    catch
    {
        return View();
    }
}

Solution

  • To return a collection from the view to the controller use indexing, to prepare required environment to be able the binding process to work properly:

    public IActionResult Index()
    {
        // Reserve maximum elements in the list
        var model = new List<FilterCondition>() { new(), new(), new() };
        return View(model);
    }
    
    [HttpPost]
    public ActionResult Submitter(IEnumerable<FilterCondition> formVals)
    {           
        ...
    }
    

    The view code:

    @model IList<Models.FilterCondition> 
    
    @{
        var items = Model;
    }
    <form asp-action="Submitter" method="post">
        <table>
            @for (int i = 0; i < items.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBox("formVals["+i+"].Code", items[i].Code)
                        </td>
                        <td>                        
                            <input type="hidden" name="@("formVals[" + i + "].FieldName")" value="@items[i].FieldName" />
                        </td>
                        <td>
                            @Html.TextBox("formVals["+i+"].FieldValue", items[i].FieldValue)
                        </td>
                        <td>
                            @Html.TextBox("formVals["+i+"].Operator", items[i].Operator)                        
                        </td>
                    </tr>
                }
    
                <tr>
                    <td>
                        <input type="submit" value="Create" />        
                    </td>
                </tr>        
        </table>
    </form>