Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# MVC Query

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.
Avatar of kaufmed
kaufmed
Flag of United States of America image

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!
@model OnlinePropertyManagement.Models.Property

Open in new window

Avatar of ukerandi

ASKER

which one i need to uncommend
public ActionResult Search()
        {
     

            Property pr = new Property();

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

        }
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?
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

I found it.
So are you really saying that no expert participating here helped you solve the issue?
ASKER CERTIFIED SOLUTION
Avatar of Prakash Samariya
Prakash Samariya
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks