Bind Data to Multiple Models From View in MVC

This article explains how to bind multiple models from a view. Sometimes the scenario may exist like this; we need to populate more than one model with the user's input. Let's implement this in a small example.

First, we will look at our model classes. Here is a small sample. The classes are fragmented within various namespaces. Anyway, we would like to bind all the classes from a single view. I have kept the ABC class within various namespaces purposefully. Just to show that it's not necessary to have all model classes within the same namespace and how strong MVC is in mapping. Haha.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  

namespace abc  
{  
    public class abcd  
    {  
        public string testabc { get; set; }  
    }  
}  

namespace MVC_Test.Models  
{  
    public class address  
    {  
        public string location { get; set; }  
    }  

    public class work  
    {  
        public string companyName { get; set; }  
    }  

    public class person  
    {  
        public string name { get; set; }  
        public string surname { get; set; }  
    }  
}  

Here is our view that will get the user's data as input. We have used an aspx view and HTML helper class to create HTML elements. If we look closely at the HTML form then we will find that we have kept the text box name the same as the property name of the class. Please keep the same name otherwise it will not bind the value, yet you will not see an error.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC_Test.Models.person>" %>  
<!DOCTYPE html>  
<html>  
<head runat="server">  
    <meta name="viewport" content="width=device-width" />  
    <title>Index</title>  
</head>  
<body>  
    <div>   
        <% using (Html.BeginForm("savedata", "person")) { %>   
            <%= Html.TextBox("name") %>  
            <%= Html.TextBox("surname") %>  
            <%= Html.TextBox("location") %>  
            <%= Html.TextBox("companyName") %>  
            <%= Html.TextBox("testabc") %>  
            <input type="submit" value="Submit Value" />  
        <% } %>  
    </div>  
</body>  
</html>

Fine, we are nearly done with our settings. We will now create a controller to receive the form submission event. In the person controller, we have defined two actions, the first action (Index) will return the view and when we click on the submit button, it will hit the save data action.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
using abc;  
using MVC_Test.Models;  

namespace MVC_Test.Controllers  
{  
    public class personController : Controller  
    {  
        public ActionResult Index()  
        {  
            return View("Index");  
        }  

        public void savedata(person p, work w, address a, abcd x)  
        {  
        }  
    }  
}  

Once we run the application and set a breakpoint on saved data, we will find that all model parameters are populated with a value.

Run the application

We have expanded the value of the p model but we will get all populated objects here.

Conclusion

In this example, we learned how to bind data to multiple models from a single view. I hope this concept will help you in your day-to-day project development life.


Similar Articles