CascadingDropDowns Webmatrix [英] CascadingDropDowns Webmatrix

查看:53
本文介绍了CascadingDropDowns Webmatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在学习本教程

So i was following this tutorial

http://www.mikesdotnetting.com/Article/196/WebMatrix-jQuery-Cascading-Dropdown-Lists

一切正常,但是当我将第一个下拉列表保存到数据库时,将 ID 传递给我,但我不知道如何传递名称而不是 ID.

Everything works nice, but when i save to the database the first dropdown, pass me the ID and i can't figure it out how to pass the name instead of the ID.

 <select id="categoryId" name="categoryId">
            <option value="">-- Select Category --</option>
             @foreach(var category in categories){
              <option value="@category.CategoryId">@category.CategoryName</option>
        }
        </select>

我知道就是这个传递的 ID,但是如果我更改它,子下拉列表将不显示任何选项.

I know that it's this that pass's the ID, but if i change it the child dropdown, doesn't show any option.

这是帮手

    var data = db.Query("SELECT CategoryId, CategoryName FROM categorias ORDER BY CategoryName");
    var categories = data.Select(item => new SelectListItem {
    Value = item.CategoryId.ToString(), 
    Text = item.CategoryName
});

这就是创建 Json 的原因

And this is whats creates the Json

@{
Layout = null;
var categoryId = UrlData[0].IsEmpty() ? 1 : UrlData[0]; 
var db = Database.Open("AppCCentro");
var sql = "SELECT ProductId, ProductName FROM produtos WHERE CategoryId = @0 ORDER BY ProductName";
var products = db.Query(sql, categoryId);
Json.Write(products, Response.Output);
}

我不知道我必须更改什么,因此它传递给选项的值选择的不是 ID,而是类别的名称.

I can't figure it out what do i have to change, so that the value that it's passed on option selected it's not the ID but the name of the category.

推荐答案

我不确定您想要完成什么,但是如果您的目标是从第一个 DropDown 中选择 CategoryName 而不是 CategoryId,您应该修改选项标签如下:

I'm not sure of what you want to accomplish, but if your goal is to optain the CategoryName instead of the CategoryId from your first DropDown you should modify the option tag as follows:

<option value="@category.CategoryName">@category.CategoryName</option>

你也应该使用

<option>@category.CategoryName</option>

因为,如果不指定值,则默认值是开始和结束标记之间的内容.

because, if you don't specify a value, the default value is the content between the opening and closing tags.

相应地,您可以按如下方式修改助手

Accordingly, you could modify the helper as follows

var data = db.Query("SELECT CategoryName FROM categorias ORDER BY CategoryName");
var categories = data.Select(item => new SelectListItem {
    Text = item.CategoryName
});

因为您不再需要 CategoryId.

because you don't need the CategoryId no more.

已编辑

显然你必须修改创建json的页面,比如

Obviously you must modify the page that creates the json, something like

@{
    Layout = null;
    var categoryId = UrlData[0].IsEmpty() ? "Beverages" : UrlData[0];
    var db = Database.Open("AppCCentro");
    var sql = @"SELECT t1.ProductId, t1.ProductName FROM produtos t1
        INNER JOIN categorias t2 ON t1.CategoryId = t2.CategoryId WHERE 
        t2.CategoryName = @0 ORDER BY ProductName";
    var products = db.Query(sql, categoryId);
    Json.Write(products, Response.Output);
}

如果/"字符包含在 CategoryName 中,您可能会遇到编码问题.

and probably you have the problem of encode the '/' character if it is contained in a CategoryName.

这篇关于CascadingDropDowns Webmatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆