Hi I'm very new to MVC
Basically what I want in webforms terms is to create a control on the master page that renders on every page associated to it, but in MVC.
So I decided that a view within a view is the best choice because I need a different model and controller than the previous view.
//Model
public class FilterViewModels
{
public Int32 CompanyID { get; set; }
public Int32 ServiceID { get; set; }
public IEnumerable<SelectListItem> Companies { get; set; }
public IEnumerable<SelectListItem> Services { get; set; }
}
//Controller
public ActionResult Filter()
{
var query = db.Companies.Select(c => new SelectListItem
{
Value = c.CompanyID.ToString(),
Text = c.Company
//,Selected = c.CompanyID.Equals(3)
});
var query1 = db.Services.Select(c => new SelectListItem
{
Value = c.ServiceID.ToString(),
Text = c.Service
});
var model = new FilterViewModels
{
Companies = query.AsEnumerable(),
Services = query1.AsEnumerable()
};
return View(model);
}
//sub view
@model SalesSystem.Models.FilterViewModels
@{
Layout = null;
}
@using (Html.BeginForm("Filter", "Filter"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.DropDownListFor(m => m.CompanyID, Model.Companies)
@Html.DropDownListFor(m => m.ServiceID, Model.Services)
}
//In Main View
@RenderPage("~/Views/Filter/Filter.cshtml");
But when I run the example I get the error: "Object reference not set to an instance of an object"
The View renders correctly in the actual view but not from the main view because it isn't running the sub views controller.
Is this the wrong way of going about what I'm trying to accomplish?