如何使用 ASP.NET MVC 实现特定于区域性的地址表单?

发布于 2024-09-09 00:56:29 字数 388 浏览 2 评论 0原文

我们目前有一个带有更新面板的 WebForms 控件。代码隐藏包含根据所选国家/地区显示/隐藏字段的逻辑。这对于 WebForms 来说很好,但是我们正在转向 MVC,而我很难解决这个问题。我还需要对其进行本地化,无论是在可本地化的资源字符串方面还是为不同国家/地区显示不同的表单字段。

目前,我们将资源字符串存储在 Resources 文件夹中的 .resx 文件中。每个国家/地区的地址字段存储在 XML 文档中,当国家/地区发生变化时,我们会加载并解析该文档。然后用它来定位相应的控件并显示/隐藏必要的控件。我要解决的最后一点是验证消息。 反射在资源类上不起作用,而且我不知道有任何实用程序允许在属性定义中使用变量。

我考虑过使用 XSL 转换来从我们的持久化创建地址位地址字段。这是一个有效的方法吗?

We currently have a WebForms control with an update panel. The code-behind contains logic to show/hide fields based on the selected country. This is fine for WebForms, but we ware moving to MVC, and I'm having a hard time sorting this out. I also need this to be localized, both in terms of localizable resource strings as well as displaying different form fields for different countries.

We currently store the resource strings in a .resx file in a Resources folder. Our address fields per country is stored in an XML doc that we load and parse when the country changes. This is then used to locate the respective controls and show/hide those necessary. The last bit I'm trying to work out is validation messages. Reflection doesn't work on Resource classes, and I don't know of any utilities to allow variables in an attribute definition.

I've thought of using an XSL transform to create the address bit from our persisted address fields. Is that a valid approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-16 00:56:29

您仍然应该能够通过访问资源本身来输出正确的语言文本。辅助方法可能会使键入更容易/更具可读性,但这将使您正确地获得表单。

为了显示不同的控件,您可以创建一个属于特定国家/地区的部分,并动态加载它们,例如:

<%= Html.RenderPartial("_addressForm-" + countryCode) %>

同样,辅助方法可能会使这更容易/更透明。

最后,您使用什么类型的验证?视图模型中的内置 MVC2 验证属性?如果是这样,我相信这是内置于您用来指定必填字段等的属性中的。

希望这会有所帮助。

You should still be able to output the correct language text just by reaching into the resource itself. A helper method might make it easier to type / more readable, but that will get you your form properly.

For displaying different controls, you could create a partial that pertains to a particular country, and load those up dynamically, something like:

<%= Html.RenderPartial("_addressForm-" + countryCode) %>

Again, a helper method might make this easier / more transparent.

Lastly, what type of validation are you using? The built-in MVC2 validation attributes off of a a view model? If so, I believe this is built-in to the attributes that you use to specify required fields, etc.

Hope this helps.

执手闯天涯 2024-09-16 00:56:29

我发现我试图对我的资源类型错误地使用反射,并且能够创建一些方法来获取正确的资源:

public string GetLabel(string control)
{
    string strResourceName;
    try
    {
        AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
        strResourceName = field.m_strName;
    }
    catch // Catch everything
    {
        return string.Empty;
    }

    if (string.IsNullOrEmpty(strResourceName))
        return string.Empty;

    return GetResource(strResourceName);
}

public string GetValidationMessage(string control)
{
    string strResourceName;
    try
    {
        AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
        strResourceName = field.m_strName;
    }
    catch // Catch everything
    {
        return Addressing.Required;
    }

    if (string.IsNullOrEmpty(strResourceName))
        return Addressing.Required;

    return GetResource(strResourceName + "Required");
}

private static string GetResource(string strResourceName)
{
    PropertyInfo property = typeof (Addressing).GetProperty(strResourceName, BindingFlags.Public | BindingFlags.Static);
    if (property == null)
        throw new InvalidOperationException("Could not locate a resource for Addressing." + strResourceName);
    if (property.PropertyType != typeof(string))
        throw new InvalidOperationException("The requested resource does not return a string.");
    return (string) property.GetValue(null, null);
}

然后在我的 Spark 视图中,我可以使用:

<li id="city" if="Model.IsCityVisible">
    <label for="City">${Model.GetLabel("City")}</label>
    ${Html.EditorFor(x => x.City)}
    !{Html.ValidationMessage("City", Model.GetValidationMessage("City"))}
</li>

I discovered I was trying to use reflection incorrectly against my resource type and was able to create a few methods to get the correct resource:

public string GetLabel(string control)
{
    string strResourceName;
    try
    {
        AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
        strResourceName = field.m_strName;
    }
    catch // Catch everything
    {
        return string.Empty;
    }

    if (string.IsNullOrEmpty(strResourceName))
        return string.Empty;

    return GetResource(strResourceName);
}

public string GetValidationMessage(string control)
{
    string strResourceName;
    try
    {
        AddressInfoField field = AddressFields.First(f => f.m_strControl == control);
        strResourceName = field.m_strName;
    }
    catch // Catch everything
    {
        return Addressing.Required;
    }

    if (string.IsNullOrEmpty(strResourceName))
        return Addressing.Required;

    return GetResource(strResourceName + "Required");
}

private static string GetResource(string strResourceName)
{
    PropertyInfo property = typeof (Addressing).GetProperty(strResourceName, BindingFlags.Public | BindingFlags.Static);
    if (property == null)
        throw new InvalidOperationException("Could not locate a resource for Addressing." + strResourceName);
    if (property.PropertyType != typeof(string))
        throw new InvalidOperationException("The requested resource does not return a string.");
    return (string) property.GetValue(null, null);
}

Then in my Spark view, I can use:

<li id="city" if="Model.IsCityVisible">
    <label for="City">${Model.GetLabel("City")}</label>
    ${Html.EditorFor(x => x.City)}
    !{Html.ValidationMessage("City", Model.GetValidationMessage("City"))}
</li>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文