部分剃刀形式的多实例 [英] Multiple instance of partial razor form

查看:85
本文介绍了部分剃刀形式的多实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个嵌套的部分控制

I have 2 nested partial controls

用户控制1:

Html.BeginRouteForm
(
   Html.RenderPartial (path of child form)
)

ChildControl:

ChildControl:

Html.LabelFor(x=>x.FirstName,"First Name")
Html.TextBoxFor(x=>x.FirstName)
...

问题:

我的主页上这部分控制的2实例。当我点击提交二审的按钮,形式使用AJAX和放大器公布; JQuery的。由于名字都需要有一个约束,形式呈现回来需要验证触发名字。这里谈到的问题:当我点击标签名,光标被集中在名字文本框部分的形式,而不是在那里验证触发二审一审

I have 2 instance of this partial control on home page. When I click on Submit button of second instance, form is posted using AJAX & JQuery. Since FirstName has a required constraint, form renders back with required validation triggered for FirstName. Here comes the issue: When I click on label "First Name", cursor gets focused on First Name TextBox of first instance of partial form instead of second instance where validation triggered.

如何解决这个任何建议?
非常感谢!

Any suggestion on how to tackle this? Thanks a lot!

推荐答案

在猜测,我会说你是一个重复的ID的受害者,因为ChildControl的两个实例可能产生相同的ID字符串。

At a guess, I'd say you're the victim of a duplicate ID, because the two instances of ChildControl are likely generating the same ID string.

不同于ASPNET,MVC似乎是生成ID本地。换句话说,没有什么幕后跟踪有多少控制生成,这是什么让ASPNET生成唯一的ID。

Unlike ASPNET, MVC appears to generate IDs "locally". In other words, nothing behind the scenes keeps track of how many "controls" are generated, which is what allows ASPNET to generate unique ids.

如果是这样,这是一个很容易解决的问题。只是你自己的ID添加到ChildControl的每个实例,做一些这样的:

If so, it's an easy enough problem to solve. Just add your own ID to each instance of ChildControl, doing something like the following:

@Html.TextBox("rel_date", Model.Value.rel_date.ToSiteString(), new { @class = "date-picker", style = "width: 75px;", id = "" })

在这里,我其实是燮$ P $上的一个文本框,我通过传递在HTML空ID值产生pssing的ID属性文本框帮手调用的参数。你会插入一些ID,你知道是唯一的页面。

Here I'm actually suppressing the ID on a textbox I'm generating by passing in an empty ID value in the html attributes parameter of the TextBox helper call. You'd insert some ID which you know to be unique to the page.

顺便说一句,您可以快速检查,看看是否我说得对,通过检查页面生成的HTML的重复的ID。当它是由在IE中只打F12(在IE9反正)。这将打开一个窗口,允许您浏览正在生成页面的HTML。可以读取它,看是否两个文本框的ID属性是一样的,这会支持我的理论。

BTW, you can quickly check to see if I'm right about the duplicate IDs by checking the generated html for the page. When it's up in IE just hit F12 (in IE9 anyway). That'll open a window which allows you to browse the html that's generating your page. You can read it to see if the ID attributes of the two textboxes are the same, which would support my theory.

祝你好运!

这篇关于部分剃刀形式的多实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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