如何在mvc 4中绑定ul List

时间:2015-08-22 13:19:39

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在使用asp.net mvc 4中的应用程序。这里我有一个名为Property as

的模型
public class Property
{
    public long ID { get; set; }
    public long Property_ID { get; set; }
    public string Images { get; set; }
    public string Title { get; set; } 
    public long PID { get; set; }
    public long Property_Type { get; set; }
    public long PropertyDetail_Type { get; set; }
    public long Country { get; set; }
    public long State { get; set; }
    public long City { get; set; }
    public string Bedroom { get; set; }
    public string Bathroom { get; set; }
    public string Location { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
    public string Area { get; set; }
    public int Status { get; set; }
    public string Purpose { get; set; }
    public long User_ID { get; set; }
    public DateTime Date { get; set; }
    public string CityName { get; set; }
    public string StateName { get; set; }
    public string CountryName { get; set; }
    public long Emp_ID { get; set; }
    public int seqnum { get; set; }
    [NotMapped]
    public SelectList PropertyList { get; set; }
    public IEnumerable<Property> PLIST { get; set; }
}

public class PropertyContext
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLCONN"].ToString());

    public IEnumerable<Property> GetPropertyList(long CityID, string Location, int Property, int Purpose, int Bedroom, int Bathroom, decimal Price1, decimal Price2)
    {            
        con.Open();
        SqlCommand cmdProperty = new SqlCommand("SP_CheckProperty", con);
        cmdProperty.Parameters.AddWithValue("@City", CityID);
        cmdProperty.Parameters.AddWithValue("@Location", Location);
        cmdProperty.Parameters.AddWithValue("@Property", Property);
        cmdProperty.Parameters.AddWithValue("@Purpose", Purpose);
        cmdProperty.Parameters.AddWithValue("@Bedroom", Bedroom);
        cmdProperty.Parameters.AddWithValue("@Bathroom", Bathroom);
        cmdProperty.Parameters.AddWithValue("@Price1", Price1);
        cmdProperty.Parameters.AddWithValue("@Price2", Price2);
        cmdProperty.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter daProperty = new SqlDataAdapter(cmdProperty);
        DataTable dtProperty = new DataTable();
        daProperty.Fill(dtProperty);

        Search obj = new Search();
        con.Close();
        return dtProperty.AsEnumerable().Select(row =>
        {
            return new Property
            {
                ID = Convert.ToInt64(row["ID"]),
                Property_ID = Convert.ToInt64(row["Property_ID"]),
                Images = Convert.ToString(row["Images"]),
                Title = Convert.ToString(row["Title"]),
                PID = Convert.ToInt64(row["PID"]),
                Property_Type = Convert.ToInt64(row["Property_Type"]),
                PropertyDetail_Type = Convert.ToInt64(row["PropertyDetail_Type"]),
                Country = Convert.ToInt64(row["Country"]),
                State = Convert.ToInt64(row["State"]),
                City = Convert.ToInt64(row["City"]),
                Bedroom = Convert.ToString(row["Bedroom"]),
                Bathroom = Convert.ToString(row["Bathroom"]),
                Location = Convert.ToString(row["Location"]),
                Price = Convert.ToDecimal(row["Price"]),
                Description = Convert.ToString(row["Description"]),
                Area = Convert.ToString(row["Area"]),
                Status = Convert.ToInt32(row["Status"]),
                User_ID = Convert.ToInt64(row["User_ID"]),
                Date = Convert.ToDateTime(row["Date"]),
                CityName = Convert.ToString(row["CityName"]),
                StateName = Convert.ToString(row["StateName"]),
                CountryName = Convert.ToString(row["CountryName"]),
                Emp_ID = Convert.ToInt64(row["Emp_ID"]),
                seqnum = Convert.ToInt32(row["seqnum"])
            };

        });

    }
}

在上面的类中有一个名为GetPropertyList的方法,它从Sql数据库中获取后获取一些参数并返回Properties列表。我有一个名为Home的控制器,它有一个名为DisplayDetails的动作,在表单提交后调用。此操作采用搜索类型的模型参数。这个名为search的模型具有相同的成员,在GetPropertyList方法中用作参数。我的行动是:

public ActionResult DisplayDetails(Search objCust)
    {

        if ((!string.IsNullOrEmpty(Request.Form["Bathroom"].ToString())) && (!string.IsNullOrEmpty(Request.Form["Bathroom"].ToString())) && (!string.IsNullOrEmpty(Request.Form["City"].ToString())) && (!string.IsNullOrEmpty(Request.Form["PropertyType"].ToString())) && (!string.IsNullOrEmpty(Request.Form["Purpose"].ToString())))
        {

            objCust.Bathroom = Convert.ToInt32((Request.Form["Bathroom"].ToString()));
            objCust.Bedroom = Convert.ToInt32((Request.Form["Bedroom"].ToString()));
            objCust.PropertyType = Convert.ToInt32((Request.Form["PropertyType"].ToString()));
            objCust.Purpose = Convert.ToInt32((Request.Form["Purpose"].ToString()));
            objCust.Maximum = Convert.ToDecimal(Request.Form["Maximum"].ToString());
            objCust.Minimum = Convert.ToDecimal((Request.Form["Minimum"].ToString()));
            objCust.Location = Convert.ToString((Request.Form["Location"].ToString()));
            objCust.City = Convert.ToInt64(Request.Form["City"].ToString());

            if (objCust.Minimum.ToString().Equals("1"))
                objCust.Minimum = 1;
            else
                objCust.Minimum = Convert.ToDecimal(objCust.Min);
            if ((objCust.Maximum.ToString().Equals("1")))
                objCust.Maximum = 1;
            else if ((objCust.Maximum.Equals("17")))
                objCust.Maximum = 1;
            else
                objCust.Maximum = Convert.ToDecimal(objCust.Max);

            PropertyContext MCon = new PropertyContext();
            Property objProperty = new Property();

            objProperty.PropertyList = new SelectList(MCon.GetPropertyList(objCust.City, objCust.Location, objCust.PropertyType, objCust.Purpose, objCust.Bedroom, objCust.Bathroom, objCust.Minimum, objCust.Maximum));

AutoComplete1.Models.IEnumerable<Property>{ objProperty.PropertyList});

            return View("DisplayDetails", new List<Property> { objProperty });                
        }
        else
        {
            Search objCity = new Search();
            objCity.Property = new SelectList(MCon.GetPropertyList(), "ID", "PropertyName");
            objCity.PurposeList = new SelectList(MCon.GetPurpose(), "ID", "PurposeName");
            return View("Default", objCity);
        }
}

我的显示详细信息视图是:

 @model IEnumerable<AutoComplete1.Models.Property>

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

<h2>DisplayDetails</h2>
<ul class="row">

@foreach (var item in Model)
{
    var @impath = item.Images;

    <li class="col-sm-4"> 
          <!--======= TAGS =========--> 
          <span class="tag font-montserrat rent"> @Html.DisplayFor(modelItem => item.Purpose)</span>
          <section> 
        <!--======= IMAGE =========-->
        <div class="img"> <img class="img-responsive" src="@impath" alt="" />
          <!--======= IMAGE HOVER =========-->
          <div class="over-proper"> <a href="#." class="btn font-montserrat">more details</a> </div>
        @*</div>*@
        <!--======= HOME INNER DETAILS =========-->
        <ul class="home-in">
          <li><span><i class="fa fa-home"></i> @Html.DisplayFor(modelItem => item.Area)</span></li>
          <li><span><i class="fa fa-bed"></i> @Html.DisplayFor(modelItem => item.Bedroom)</span></li>
          <li><span><i class="fa fa-tty"></i> @Html.DisplayFor(modelItem => item.Bathroom)</span></li>
        </ul>
        <!--======= HOME DETAILS =========-->
        <div class="detail-sec"> <a href="#." class="font-montserrat">sweet home for small family</a> <span class="locate"><i class="fa fa-map-marker"></i> @Html.DisplayFor(modelItem => item.Location), @Html.DisplayFor(modelItem => item.CityName)</span>
          <p>@Html.DisplayFor(modelItem => item.Description) </p>
          <div class="share-p"> <span class="price font-montserrat">@Html.DisplayFor(modelItem => item.Price)</span> <i class="fa fa-star-o"></i> <i class="fa fa-share-alt"></i> </div>
        </div>
      </section>
</li>

}

</ul>

这里的一切都运转良好。控制器操作DisplayDetails在表单提交上获得正确的参数。正确的Prroperty List来自数据库。但是当我将列表发送到视图时,它没有绑定到正确的值。任何人都可以在这里帮助我。

0 个答案:

没有答案