Solved

C# MVC Query

Posted on 2016-08-10
11
36 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 74

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Author Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

18 Experts available now in Live!

Get 1:1 Help Now