Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
public ActionResult Index()
{
DDlCombined ddlC = new DDlCombined();
return View(ddlC.GetDistricts());
//return View(registerForm);
}


Quote:
public class DDlCombined
{
public int? SelectedDistrictId { get; set; }
public int? SelectedTehsilId { get; set; }


public IEnumerable<district> GetDistricts()
{
FarmerDBContext farmerDBContext = new FarmerDBContext();
return farmerDBContext.Districts.ToList();
}

public IEnumerable<tehsil> GetTehsil()
{
FarmerDBContext farmerDBContext = new FarmerDBContext();
return farmerDBContext.Tehsils.ToList();
}
}


Quote:

@model FarmerRegisterFormAndPrint.Models.DDLCombined

@{
ViewBag.Title = "Index";
}

<link href="~/Content/StyleSheet.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

@Html.Partial("_LayoutFarmer")




@using (Html.BeginForm("Details", "Details", FormMethod.Post))
{

Quote:
Html.Label("*Name of District")
Html.Partial("_District", Model)
Html.Label("*Name of Tehsil")
Html.Partial("_Tehsil", Model)




My view is strongly bind to DDLCombined class but when i try to pass the list<district>from controller to View it gives error as following:

Quote:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FarmerRegisterFormAndPrint.Models.District]', but this dictionary requires a model item of type 'FarmerRegisterFormAndPrint.Models.DDlCombined'.
Posted
Comments
Timberbird 14-Jul-15 5:56am    
You are passing ddlC.GetDistricts(), which is of type IEnumerable (and actually is a List according to public GetDistricts() method implementation), while the model accepts @model FarmerRegisterFormAndPrint.Models.DDLCombined. I guess the error is expected in this case

1 solution

I think you need to use multiple models in single view..

use this link
Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900