如何在不使用隐藏字段的情况下从 JQuery 访问视图数据?
我正在 CreateInstruction 视图中保存“指令”。该视图有部分视图“CreateInstnAttribute”,它保存与该指令相关的“属性”。它需要 InstnID 作为外键。使用 JQuery 保存指令以避免回发。那么如何获取InstnID来保存InstnAttribute呢?我尝试使用 HiddenField 保存 InstnId 但无法在那里接收值。我们可以直接在JQuery中使用viewdata吗?任何帮助都将非常重要。
I am saving "Instruction" in CreateInstruction view. This view has partial view "CreateInstnAttribute" which saves "Attributes" related to this Instruction. It requires the InstnID as foreign key. The Instruction is saved using JQuery to avoid postback. So how can I get the InstnID to save the InstnAttribute? I tried HiddenField to save InstnId but I am not able to receive the value there. Can we use viewdata directly in JQuery? Any help will be appreciable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 jQuery 是在标记(.aspx 文件)中定义的,您可以通过将变量分配给其值来访问视图数据,如下所示。
警报呼叫只是为了表明您拥有它。显然你可以按照你的意愿使用它。
If your jQuery is defined in the markup (.aspx file) you can access the view data by assigning a variable to its value as follows.
The alert call is just to show that you have it. Obviously you can use it as you wish.
您需要在 PartialView 中传递 InstId 并在 PartialView 内部访问它。最好的方法是创建一个 AttributeViewModel(其中包含 InstID)并将其传递到局部视图。
jQuery 可以使用内联代码访问它,如下所示:
You need to pass the InstId in the PartialView and access it inside the PartialView. The best way to is to create a AttributeViewModel (which would contain the InstID) and pass it down the partial view.
jQuery can access it using inline code as :
试试这个代码
try this code
我所有的 PartialViews 就像你所说的那样都遵循相同的基础:
这篇很酷的文章:)
它们有点复杂,但这是我开始的基础,你会得到一切工作没时间!
All my PartialViews like the one you are talking follow the same base:
this cool article :)
they come up a little more complex, but that was the base I started with, and you will get everything work in no time!