0

我是 MVC 实体框架的新手,我的模型是一个实体框架模型,我有一个与省表有关系的商业表,而省表与国家表有关系。

因此,当我要创建一个 Commerce 时,我应该能够先选择国家,然后再选择属于该国家的省份。

到目前为止,我已经设法显示了国家和所有省份,但是一旦选择了一个国家,我不知道如何更改省份......

我看过一些帖子,其中解释了如何使用剃须刀(我正在使用.net)创建模型(首先编写代码或类似的东西)来做这样的事情

如果您能在这方面帮助我,我将不胜感激。

4

2 回答 2

0

Using Jquery would be the best solution for such situation.even if you are not using it yet. saving a postback would be worth it. Here is one example with State(parent) and County(child) relationship.

    <asp:DropDownList ID="ddlState" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlCounty" runat="server">
</asp:DropDownList>

Here is the jquery code to implement cascaded dropdown list.

        $(document).ready(function () {

        $("#<%= ddlState.ClientID %>").change(function () {

            var sourceddl = "<%= ddlState.ClientID %>";

            var stateid = $("#<%= ddlState.ClientID %> option:selected").val();
            var Stateid = { Stateid: stateid };

            $.ajax({
                type: 'POST',
                url: 'CacheSample.aspx/GetCounties',
                data: JSON.stringify(Stateid),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {

                    if (data.d) {                     

                    var options = [];                     
                                        if (result.d) {
                                            for (var i = 0; i < result.d.length; i++) {
                                                options.push('<option value="',
                                              result.d[i].countyID, '">',
                                              result.d[i].countyName, '</option>');
                                            }

                                            $("#<%= ddlCounty.ClientID %>").html(options.join(''));
                                        }                        
                     }
                },
                error: function () {
                    alert("Error! Try again...");
                }
            });

        });

    });

I am using a webmethod to retireive the Counties for a selected state.

[WebMethod]
public static County[] GetCounties(int Stateid)
{
    County[] countiesArr = StatesCountyModel.GetCountyForState(Stateid).ToArray();
    return countiesArr;     
}

I guess it should help you. If you are new to Jquery please let me know. You just need to include few javascript files into your project and you can use this code.

Praveen

于 2011-11-28T11:01:55.487 回答
0

简短的回答是您将回发并过滤省份,或者使用 jQuery 并即时执行。

我自己,我会回帖......但这只是我。

于 2011-11-26T22:51:16.220 回答