ukerandi
asked on
C# MVC Query
Hi,
I have write following code for Model
Model
Controller
View
Im getting Following Error
The model item passed into the dictionary is of type 'OnlinePropertyManagement. Models.Pro perty', but this dictionary requires a model item of type 'System.Collections.Generi c.IEnumera ble`1[Onli neProperty Management .Models.Pr operty]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationExc eption: The model item passed into the dictionary is of type 'OnlinePropertyManagement. Models.Pro perty', but this dictionary requires a model item of type 'System.Collections.Generi c.IEnumera ble`1[Onli neProperty Management .Models.Pr operty]'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
I have write following code for Model
Model
public string propertyID { get; set; }
public void SearchDetails()
{
SqlConnection con = new SqlConnection(connstring);
con.Open();
SqlCommand cmdSearchDeatils = con.CreateCommand();
cmdSearchDeatils.CommandText = "SELECT * FROM [Property_Vw]";
SqlDataReader rdrDetails = cmdSearchDeatils.ExecuteReader();
while (rdrDetails.Read())
{
propertyID = rdrDetails["PropertyID"].ToString();
}
rdrDetails.Close();
cmdSearchDeatils.Dispose();
con.Close();
}
}
Controller
public ActionResult Search()
{
/*
var viewModel = new Property()
{
propertyID = "1"
};
List<Property> viewModelList = new List<Property>();
viewModelList.Add(viewModel);
return View(viewModelList);
* */
Property pr = new Property();
pr.SearchDetails();
return View(pr);
}
View
@model IEnumerable<OnlinePropertyManagement.Models.Property>
@{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var product in Model)
{
@product.propertyID
}
Im getting Following Error
The model item passed into the dictionary is of type 'OnlinePropertyManagement.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationExc
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
The error is clear: Your view expects a collection of Property, but you are only passing one. I think you need to use the commented-out code instead. Either that, or you need to update your view to expect only a single Property rather than a collection.
According to your logic, you are mixing listing and single object code!
If you want to make listing view, your view is correct and need to change the controller to returns the list of object
Note: to do that, uncomment commented code! (as said by käµfm³d)
If you want to make single object view, then change the code below in view for model only!
If you want to make listing view, your view is correct and need to change the controller to returns the list of object
Note: to do that, uncomment commented code! (as said by käµfm³d)
If you want to make single object view, then change the code below in view for model only!
@model OnlinePropertyManagement.Models.Property
ASKER
which one i need to uncommend
public ActionResult Search()
{
Property pr = new Property();
pr.SearchDetails();
return View(pr);
}
public ActionResult Search()
{
Property pr = new Property();
pr.SearchDetails();
return View(pr);
}
ASKER
Hi,
Used Following Model code,I need return all the records
View
But No errors No Records.
Any idea?
Used Following Model code,I need return all the records
public ActionResult Search()
{
var viewModel = new Property();
List<Property> viewModelList = new List<Property>();
viewModelList.Add(viewModel);
return View(viewModelList);
}
View
@model IEnumerable< OnlinePropertyManagement.Models.Property>
@{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var rtest in Model)
{
@rtest.propertyID
}
But No errors No Records.
Any idea?
ASKER
Hi,
Used Following Model code,I need return all the records
I'm struggling how to add SearchDetails() function to the list
Model
Used Following Model code,I need return all the records
I'm struggling how to add SearchDetails() function to the list
public ActionResult Search()
{
var viewModel = new Property();
List<Property> viewModelList = new List<Property>();
viewModelList.Add(viewModel);
return View(viewModelList);
}
Model
@model IEnumerable< OnlinePropertyManagement.Models.Property>
@{
ViewBag.Title = "Search";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@foreach (var rtest in Model)
{
@rtest.propertyID
}
ASKER
I found it.
So are you really saying that no expert participating here helped you solve the issue?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My apologies!!! I didn't cleary mentioned what i need.
I knew it's only one records coming.But i need to display all the records.
What i did is in my Modelchange Showdetails to List return
public List<Property> SearchDetails()
{
///code
}
Anyway thank you very much for all the comments
I knew it's only one records coming.But i need to display all the records.
What i did is in my Modelchange Showdetails to List return
public List<Property> SearchDetails()
{
///code
}
Anyway thank you very much for all the comments
ASKER
Thanks