1

Thanks in advance.

Can anyone please help me get this button working so I can display a row from the Northwind database 'only' when the button is clicked.

I have been doing webgrid tutorials; however, in each tutorial the grid/ webgrid is always showing before it is searched.

It would be great if I could learn how to use a button to show data from a row in the Northwind database in a list/label/table or grid 'only' when a form is submitted.

Keeping it simple, I have this:

@{
    Layout = "~/_Layout.cshtml";
    Page.Title = "Compare";

   var db = Database.Open("Northwind");
   var selectedData = db.Query("SELECT * FROM Products");

<h1>Products</h1>

 <form action="" method="post">
    <div>
      <input type="Submit" value="Get Products" /><br/> <br/> 
    </div>
 </form>

    <ol>
       @foreach(var row in selectedData)
       {
        <li><a href="#">@row.ProductName</a></li>
       }
     </ol>
  }

EDIT: In Mike's tutorial (at: http://www.mikesdotnetting.com/Article/211/Adding-A-Footer-To-The-Razor-WebGrid) Mike uses a webgrid in Razor to access the Northwind database and display orders.

I would like to use form data to access the database with a submit button; is this possible please? If so how? Thanks.

4

3 回答 3

1

感谢大家的意见。我设法在 Mikes 教程(在我上面的帖子中提到)的 Razor 代码中添加了一个简单的按钮,并且它可以工作。

这是 Mike 的代码以及我添加的按钮:

@{
    Layout = "~/_Layout.cshtml";
    Page.Title = "WebGrid.Table method";

    var db = Database.Open("Northwind");
    var sql = "SELECT OrderId FROM Orders";
    var orders = db.Query(sql).Select(o => new SelectListItem {
        Value = o.OrderId.ToString(), 
        Text = o.OrderID.ToString(),
        Selected = o.OrderID.ToString() == Request["OrderID"]
    });

    WebGrid grid = null;
    var orderTotal = 0f;

    if(IsPost){
        sql = @"SELECT p.ProductName, o.UnitPrice, o.Quantity, 
                (o.UnitPrice * o.Quantity) - (o.UnitPrice * o.Quantity * o.Discount) As TotalCost 
                FROM OrderDetails o INNER JOIN Products p ON o.ProductID = p.ProductID 
                WHERE o.OrderID = @0";

        var orderDetails = db.Query(sql, Request["OrderID"]);
        orderTotal = orderDetails.Sum(o => (float)o.TotalCost);

        grid = new WebGrid(orderDetails, canPage: false, canSort: false);
    }
}
<h1>@Page.Title</h1>
<form method="post">
    @Html.DropDownList("OrderID", orders)
    <input type="Submit" value="Get Products" /><br/> <br/> 
</form>

@if(grid != null){
    @grid.Table(
            columns: grid.Columns(
                grid.Column("ProductName", "Product", style: "_220"),
                grid.Column("UnitPrice", "Price", style: "_60", format: @<text>@item.UnitPrice.ToString("c")</text>),
                grid.Column("Quantity", style: "_90"),
                grid.Column("TotalCost", "Total Cost", style: "_90", format: @<text>@item.TotalCost.ToString("c")</text>)
            ), 
            footer: @<table class="footer">
                         <tr>
                             <td class="_220">Total</td>
                             <td colspan="2" class="_150">&nbsp;</td>
                             <td class="_90">@orderTotal.ToString("c")</td>
                         </tr>
                    </table>);
}
于 2013-03-30T15:45:36.413 回答
1

如果您使用的是 ASP.NET MVC,那么使用它的目的是将应用程序划分为三种组件……您在那里拥有的视图,处理来自和的请求的控制器到模型,最后是模型,这是您应该首先放置查询的地方。

如果您没有全部使用 MVC,那么使用它是没有意义的。

于 2013-03-30T06:16:42.500 回答
0

这是我一起破解的一个粗略的例子。

当您第一次在 处加载页面时http://yourDomain/Product/Index,您只得到一个文本框和搜索按钮。当您在文本框中输入几个字母并单击搜索按钮时,您会得到一个以输入字母开头的 Northwind 产品列表。

这是 ~/Controllers/ProductController.cs 文件的内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index(String startString)
        {
            List<Models.Product> theProducts;
            using (
                Models.NorthwindEntities NWC = 
                new Models.NorthwindEntities()
            )
            {
                 theProducts =
                    (from e in NWC.Products
                     where e.ProductName.StartsWith(startString)
                    select e).ToList();
            }
            return View(theProducts);
        }

    }
}


这是 ~/Views/Product/Index.cshtml 文件的内容:

@model List<MvcApplication1.Models.Product>

@{
    ViewBag.Title = "Product";
}

<h2>Product</h2>

@* Use a very crude means to dump matching products out *@ 
@foreach (MvcApplication1.Models.Product p in Model) {
    @Html.Raw(p.ProductName + "<br />");
}
<br />
@* See this for example use of form and submit button
http://weblogs.asp.net/raduenuca/archive/2011/03/13/asp-net-mvc-cascading-dropdown-lists-tutorial-part-2-cascading-using-normal-form-post-html-beginform-helper.aspx *@
@using (Html.BeginForm("Index", "Product", FormMethod.Get)) { 
    @Html.Raw("Enter Search Text Here:<br />");
    <input type="text" name="startString" id="txtSearch" /><br />
    <input type="submit" value="Click Hear to Search" />
}
于 2013-03-30T07:51:00.653 回答