Share via

Multi-row Model comes null

Ali Yılmaz 81 Reputation points
2022-08-09T18:08:47.22+00:00

Hi,

I am returning 50-60 rows of data. I will add this data to the database with the model. But the model is returning null. What is the cause of this and what is the solution.

  for (int i = 0; i < Model.Data.Count; i++)  
                                    {  
                                <tr class="bg-light text-white">  
  
                                    <td>  
                                        <input value="@Model.Data[i].Data" name="[@i].Data" size="20" type="text">  
                                    </td>  
  
                                    <td style="display:none">  
                                        <input value="@Model.Data[i].Data2" name="[@i].Data2" size="10" type="text">  
                                    </td>  
  
                                    <td>      
                                       
}  

  [HttpPost]  
        public async Task<IActionResult> DusumAdd(List<VM_Model> vM_Model)  
        {  
  
        }  
Developer technologies | ASP.NET Core | Other

Answer accepted by question author

AgaveJoe 31,361 Reputation points
2022-08-09T22:15:38.817+00:00

What is the cause of this and what is the solution.

The most common issue is the model property names do not match input names. The model binder cannot match the model properties to the post collection. Your code clearly shows the property name that populated the forum is VM_Model.Data[0].Data but you named the input [0].Data.

Unfortunately you did not share the most import bit of code which is the VM_Model model and the model that populates the form. We can only guess how your code works.

The following is a working example that uses the input name [0].Data . Maybe this code will help you figure out how to fix your code.

@model MvcBasic.Controllers.VM_Model  
  
@{  
    ViewData["Title"] = "Index";  
}  
  
<h1>Index</h1>  
  
<h4>FromViewModel</h4>  
<hr />  
<div class="row">  
    <div class="col-md-4">  
        <form asp-action="Index">  
  
            <table>  
                @for (int i = 0; i < Model.Data.Count; i++)  
                {  
                <tr class="bg-light text-white">  
  
                    <td>  
                        <input value="@Model.Data[i]" name="[@i].Data" size="20" type="text">  
                    </td>  
  
                    <td style="display:none">  
                        <input value="@Model.Data[i]" name="[@i].Data2" size="10" type="text">  
                    </td>  
  
                    <td>  
                </tr>  
                }  
            </table>  
            <input type="submit" value="submit" />  
        </form>  
    </div>  
</div>  

Actions and model

    public class VM_Model  
    {  
        public List<string>? Data { get; set; }  
        public List<string>? Data2 { get; set; }  
    }  
    public class FormController : Controller  
    {  
        // GET: FormController1  
        [HttpGet]  
        public ActionResult Index()  
        {  
            VM_Model model = PopulateModel();  
            return View(model);  
        }  
  
        [HttpPost]  
        public ActionResult Index(List<VM_Model> model)  
        {  
            return Ok(model);  
        }  
  
        public VM_Model PopulateModel()  
        {  
            List<string> data = new List<string>();   
            List<string> data2 = new List<string>();  
  
  
            for (var i=0; i<10; i++)  
            {  
                data.Add($"data{i}");  
                data2.Add($"data2{1}");  
            }  
  
            VM_Model model = new VM_Model();  
            model.Data = data;  
            model.Data2 = data2;  
  
            return model;  
        }  
    }  

Next time, please post all the relevant code. This means you might have to make an small effort to write a sample if your code is too complex to post. For example, the code above target model binding.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Michael Taylor 61,226 Reputation points
    2022-08-09T19:46:05.98+00:00

    Did you wrap this in a form? What does the browser show as the data being passed back to the server as the request body? At this point we'll be just guessing as to what you're seeing.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.