使用MVC3&放3下拉列表级联; LinqSql [英] 3 Dropdown List cascade using MVC3 & LinqSql

查看:167
本文介绍了使用MVC3&放3下拉列表级联; LinqSql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个DropDownList的我想打3的DropDownList与级联。我使用LinqSql数据库。

I have 3 dropdownlist i wanna make 3 dropdownlist with cascade. I am using LinqSql for database..

我有3个表产品(编号,名称),设计(ID,master_id,名称),型号(ID,design_id,名)
master_id结合到产品(ID),design_id势必设计(ID)..

I have 3 tables Product(id,name), Design(id,master_id,name), Model(id,design_id,name) master_id bound to Product(id), design_id bound to Design(id)..

我要创建一个下拉这是将要呈现的产品和比我选择一个产品它会让设计下拉使否则会留下残疾..这里也就是我不能解决了棘手的部分,我需要在很好的解释在这里创造它会被直到选择了设计通常禁用3下拉。

I want to create one dropdown which is gonna show Products and than when i choose a product its gonna make Design dropdown enabled else it will stay disabled.. also here is the tricky part that i couldnt solve and i need great explanation in here creating 3rd dropdown which is gonna be disabled normally till a Design is chosen.

每个人要填充势必them.Its像一个较低的DropDownList;
产品要启用和填充设计,
设计要启用和填充模型。
我可以用2下拉菜单做,而是当它涉及到3下拉我坚持实在太差了对即时通讯(脑冻结)。

Each of them gonna populate a lower dropdownlist bound to them.Its like; Product gonna enable and populate Design, Design gonna enable and populate Model. I can do it with 2 dropdowns but when it comes to 3 dropdown i stuck really badly im on (brain-freeze)..

我已经检查了其他问题不可能找到我自己的解决方案。正如我所说的即时通讯使用LinqSql我需要大约3 cascadingdropdown列表这种类型的数据范围的解决方案。

I already checked the other questions couldnt find any solution for my self. As i said im using LinqSql i need a solution about 3 cascadingdropdown list for this type of data reach.

由于已经是什么u能做到!如果ü可以解释模型 - 视图 - 控制器泛音和参数,以及为什么使用它们,将是真棒。 IAM还挺初学者在这个MVC3。

thanks already for anything u can do! and if u can explain Model-View-Controller partials and the parameters and why you use them that would be awesome. Iam kinda beginner at this MVC3.

推荐答案

我想接近这样的问题:

首先,在控制器中,我们将设置有以下几种方法:

First, in the controller, we'll set up have the following methods:

public JsonResult GetDesignsForProduct(int productId)
{
  // Instantiate our context and do whatever goo we need to select the objects we want
  using (MyDatabaseContext ctx = new MyDatabaseContext())
  {
     return Json(ctx.Designs.Where(d => d.master_id == productId).ToList(), JsonRequestBehavior.AllowGet);
  }
}

public JsonResult GetModelsForDesign(int designId)
{
  // Instantiate our context and do whatever goo we need to select the objects we want
  using (MyDatabaseContext ctx = new MyDatabaseContext())
  {
     return Json(ctx.Models.Where(d => d.design_id == designId).ToList(), JsonRequestBehavior.AllowGet);
  }
}

我打开得在这里;如果您的数据包含敏感信息 - 用户名/ E-mail地址,其他所有权或法律保护的数据,等等 - 你可以更改为只允许后,并相应地修改你的Javascript。请参见菲尔哈克的文章

此外,如果你想到这个数据经常变动,这些方法将在默认情况下,根据应用程序的缓存设置缓存它。您可以在方法中添加的OutputCache 属性来改变这种行为。

Also, if you expect this data to change frequently, these methods will cache it by default according to your application's cache settings. You can add an OutputCache attribute on the method to alter this behavior.

然后,在视图中,您将有一些AJAX管道,是这样的:

Then, in the view you'll have some AJAX plumbing, something like this:

function LoadDesigns() {
    // Get the currently-selected value in our Product dropdown
    var prod = $("#Product").val();

    // Call our controller method and process the list of Design objects
    $.getJSON('@Url.Content("~/ControllerName/GetDesignsForProduct")', { productId: prod },
        function (designs) {
            $("#Design").empty();
            $.each(designs, function (i, c) {
                $("#Design").append(
                    $('<option></option>').val(c.id).html(c.name)
                );
            });
    });
}

function LoadModels() {
    // Get the currently-selected value in our Design dropdown
    var des = $("#Design").val();

    // Call our controller method and process the list of Model objects
    $.getJSON('@Url.Content("~/ControllerName/GetModelsForDesign")', { designId: des },
        function (models) {
            $("#Model").empty();
            $.each(models, function (i, c) {
                $("#Model").append(
                    $('<option></option>').val(c.id).html(c.name)
                );
            });
    });
}

最后,定义所有三个下拉列表如下:

Finally, define all three drop-downs as follows:

@Html.DropDownList("Product", productSelectList, new { onchange = "LoadDesigns()" })
@Html.DropDownList("Design", null, new { onchange = "LoadModels()" })
@Html.DropDownList("Model")

不要忘记,HTML辅助实际上只是快捷方式来生成底层的HTML,并在剃刀你经常只是直接进入HTML而不是与助手搞乱。所以,你可以很容易地写这些为:

Don't forget that the HTML helpers are really just shortcuts to generate the underlying HTML, and in Razor you frequently just go straight to HTML instead of messing with the helpers. So you could just as easily write these as:

<select id="Product" onchange="LoadDesigns()">
  @foreach (var prod in products) {
    <option value="@prod.id">@prod.name</option>
  }
</select>

<select id="Design" onchange="LoadModels()"></select>

<select id="Model"></select>

这篇关于使用MVC3&放3下拉列表级联; LinqSql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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