MVC入门小示例---淘宝商品的搜索和价格筛选

     五一前后一直在忙安卓的东西,直到现在终于有机会喘口气了,于是就抽空写了这篇博文,来记录我之前学习MVC一周后所写的小例子:搜索淘宝商品并对商品进行价格筛选。

     先上开始界面:

   

     这个界面的源码如下:

@{
    ViewBag.Title = "主页";
}

 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    <fieldset>

        <legend>Search</legend>
        <div id ="editor-field">
            <p>搜索商品 @Html.TextBox("keyword")</p> 
            <p>价格上限 @Html.TextBox("max")</p>
            <p>价格下限 @Html.TextBox("min")</p>
        </div>
    </fieldset>
        <p>
            <input type="submit" value="Search" />
        </p>
}

      值得注意的是我标为红色的代码,TextBox提供用户输入,并且提交用户输入,括号里的"keyword"是用来标识所提交的内容,方便我们从提交的表单中提取出用户的输入。
      然后就是我们的主程序:

 public class HomeController : Controller
    {
        string keyword;
        double max;
        double min;
        //
        // POST: /Index/
        public ActionResult Index(FormCollection values)
        {
            keyword = values["keyword"];
            if (values["max"] != null)
            {
                max = double.Parse(values["max"]);
            }
            if (values["min"] != null)
            {
                min = double.Parse(values["min"]);
            }
            if (keyword == null)
            {

                return View();
            }
            else
            {
                List<Goods> goodsList = new List<Goods>();
                GoodsInformation information = new GoodsInformation(keyword, max, min);
                List<string> imgs = information.getImg();
                List<string> hrefs = information.getHref();
                List<string> prices = information.getPrice();
                for (int i = 0, len = prices.Count; i < len; i++)
                {
                    Goods goods = new Goods();
                    goods.Imge = imgs.ElementAt(i);
                    goods.href = hrefs.ElementAt(i );
                    goods.Name = keyword;
                    goods.Price = double.Parse(prices.ElementAt(i));
                    goodsList.Add(goods);
                }
                return View("Search", "_Layout", goodsList);
            }
        }
    }

      注意Index操作的参数,也就是我标为红色的部分,它说明我们这个操作是提取所POST的表单并进行处理,所以最好注释说明这是一个POST请求。

       我们提交的表单其实就是键值对的集合,当然,我们在使用的时候,必须注意非空的判断,因为用户可能什么也没输入就提交了。这里我还没有使用验证,当用户没有输入关键字,就会重新显示搜索界面。如果用户输入相关信息并且提交,就会跳转到另一个View---Search。

       我们来输入一些关键字吧!

     

       
     看看我们的搜索结果是怎样的:

    

      抱歉,由于我没有时间对显示的界面进行排版,所以暂时无法提供完整的商品列表,只好截取出一部分出来。

      嗯,MG 独角兽还是相对较贵的。

      Search的view的源码如:

@model List<TaoBaoShop.Models.Goods>

@{
    ViewBag.Title = "搜索";
}
<h3>搜索 @Model.ElementAt(0).Name 的结果列表:</h3>
<ul>
    @foreach (var m in Model)
    { 
      <li>
        <a href="@m.href">
          <span><img alt="@m.Name" src="@m.Imge"/></span><br />
          <span>总价:@m.Price</span></a><br /><br />
     </li>
    }
 </ul>    

      我们在操作中传递一个模型给相应的视图,然后就可以在视图中使用该模型。
      我们的商品列表的界面是根据价格进行排列的,这样的实现并不难,只要在提取商品价格西信息的同时进行排序就可以了。

      要想呈现商品列表,最主要的部分就是商品信息的提取:

string handleUrl(string content)
        {
            string strHtml = "";
            WebResponse wrp = null;
            try
            {
                WebRequest wrq = WebRequest.Create(content);
                wrp = wrq.GetResponse();
            }
            catch (WebException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (wrp != null)
                {
                    StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.GetEncoding("gbk"));
                    strHtml += sr.ReadToEnd();
                    sr.Close();
                    wrp.Close();
                }
                else
                {
                    mIsLeft = false;
                }
            }
            return strHtml;
        }

      得到淘宝网页内容后,就是利用正则表达式对商品信息进行提取了。
      例子非常简单,还请各位见笑。

 

 

   

posted @ 2013-05-06 15:42  文酱  阅读(2678)  评论(5编辑  收藏  举报