横跨两个相邻的HTML表单形式MVC3重复绑定 [英] MVC3 Form binding duplicated across two neighbouring html forms

查看:128
本文介绍了横跨两个相邻的HTML表单形式MVC3重复绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我旁边有一个登记表的登录表单,登录表单是一个永久性(直到很久进一步发展),有时登记为主要内容与此登录表单一致。

I have a scenario where I have a login form next to a registration form, as the login form is a permanent fixture (until much further development), and sometimes registration as the main content coincides with this login form.

现在我们进入模糊地带:这两种形式具有不同控制器上的不同的动作,但他们都有两个共同点,是他们都有一个的ValidationSummary ,他们都有一个称为字段用户名。如果我造成服务器端验证错误,例如当用户未选中接受使用条款,用

Now we enter the twilight zone: Both forms have different actions on different controllers, but they share two things in common, being they both have a ValidationSummary, and they both have a field called UserName. If I cause a server side validation error, e.g. when the user hasn't ticked 'Accept terms of use', using

ModelState.AddModelError("", "You must accept the Terms and Conditions to become a member.");

中显示的这两种形式的验证摘要,和新的用户名的错误,即试图注册的用户,则显示在用户名字段上这两种形式。表格甚至不共享一个共同的视图模型的键入的,更不用说实例。所有我可以问,使这个正确的一个问题是,跆拳道?

the error is displayed in the validation summaries of both forms, and the new user name, i.e. the user trying to register, is displayed in the UserName field on both forms. The forms don't even share a common view model type, let alone instance. All I can ask to make this properly a question is WTF?

推荐答案

如果您的HTML元素有相同的ID你可能期望这种行为。我认为,这个最简单的解决方法是以您的登录用户名ID名称重命名为一个更明确的有一个像LoginUserName。这样,你是不是混合两种不同的东西。

If your html elements have the same Id you might expect this kind of behaviors. I think that the easiest solution for this is to rename your login username id name into a more explicit one like LoginUserName. this way you are not mixing two different things.

第二,我建议你(如果你还没有这样做)封装登录功能集成到一个单独的形式(如适用),因为你会被发送整个页面,包括不适用于使用案例登录操作。

Second, I suggest you (if you haven't done so) to encapsulate your login functionality into a separate form (if that applies) because you would be sending the whole page, including use cases that don't apply to a login action.

这篇关于横跨两个相邻的HTML表单形式MVC3重复绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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