Solved

C# MVC Query

Posted on 2016-08-10
11
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 10

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 10

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

626 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