Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# MVC Query

Posted on 2016-08-10
11
Medium Priority
?
72 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
10 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

783 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