Solved

C# MVC Query

Posted on 2016-08-10
11
43 Views
Last Modified: 2016-08-12
Hi,
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();
          
        }


    }

Open in new window


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);

        }

Open in new window



View
@model IEnumerable<OnlinePropertyManagement.Models.Property>

@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 @foreach (var product in Model)
 {
     @product.propertyID
 }

Open in new window


Im getting Following Error


The model item passed into the dictionary is of type 'OnlinePropertyManagement.Models.Property', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[OnlinePropertyManagement.Models.Property]'.

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.InvalidOperationException: The model item passed into the dictionary is of type 'OnlinePropertyManagement.Models.Property', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[OnlinePropertyManagement.Models.Property]'.

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.
0
Comment
Question by:ukerandi
  • 6
  • 2
  • 2
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41751378
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.
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41751501
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!
@model OnlinePropertyManagement.Models.Property

Open in new window

0
 
LVL 10

Author Comment

by:ukerandi
ID: 41752125
which one i need to uncommend
public ActionResult Search()
        {
     

            Property pr = new Property();

            pr.SearchDetails();
            return View(pr);

        }
0
 
LVL 10

Author Comment

by:ukerandi
ID: 41753209
Hi,
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);
          
           
        }

Open in new window


View
@model IEnumerable< OnlinePropertyManagement.Models.Property>

@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@foreach (var rtest in Model)
{ 

    @rtest.propertyID
}
   

Open in new window


But No errors No Records.

Any idea?
0
 
LVL 10

Author Comment

by:ukerandi
ID: 41753218
Hi,
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);
          
           
        }

Open in new window



Model
@model IEnumerable< OnlinePropertyManagement.Models.Property>

@{
    ViewBag.Title = "Search";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@foreach (var rtest in Model)
{ 

    @rtest.propertyID
}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Author Comment

by:ukerandi
ID: 41753233
I found it.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41753281
So are you really saying that no expert participating here helped you solve the issue?
0
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41753381
I have clearly mentioned about your two case,
1) if you want object list in model, and
2) single object in model!!!
See comment : #ID: 41751501

It is bad, that some people do not appreciate!
0
 
LVL 10

Author Comment

by:ukerandi
ID: 41753458
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
0
 
LVL 10

Author Closing Comment

by:ukerandi
ID: 41753459
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now