Here's one (very simplified) way to do it. You need to use ViewModels.
public class PageViewModel
{
public string Title { .. }
public string Content { .. }
public MyFormViewModel MyForm { .. }
}
public class MyFormViewModel
{
public string Name { .. }
public string Address { .. }
// ... so on
}
Your Main Page View:
@model PageViewModel
<h1>@Model.Title</h1>
<p>@Model.Content</p>
<div class="my-form">
@Html.Partial("_MyForm", Model.MyForm)
</div>
Your Form Partial ("_MyForm")
@model MyFormViewModel
@using(Html.BeginForm())
{
<label>Name</label>
@Html.TextBoxFor(m => m.Name)
}
Your Controller Actions:
// Load Page
[HttpGet]
public ActionResult MyPage()
{
var viewModel = new PageViewModel();
viewModel.MyForm = new MyFormViewModel();
return View(viewModel);
}
// SubmitForm: Accepts MyFormViewModel
[HttpPost]
public ActionResult MyPage(MyFormViewModel viewModel)
{
// do something with form data
}
On your main page view, you'll notice @Html.Partial(). This loads the partial with a model's property which in this case is MyForm. Due to this, your partial will have an @model of MyFormViewModel.
HtmlFieldPrefix(refer this answer for an example). Alternatively you can use customEditorTemplatesfor the typeof property.