如何在不使用隐藏字段的情况下从 JQuery 访问视图数据?

发布于 2024-10-03 15:58:41 字数 230 浏览 0 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(4

大姐,你呐 2024-10-10 15:58:41

如果您的 jQuery 是在标记(.aspx 文件)中定义的,您可以通过将变量分配给其值来访问视图数据,如下所示。

var myValue = "<%= ViewData["MyValue"] %>";
alert(myValue);

警报呼叫只是为了表明您拥有它。显然你可以按照你的意愿使用它。

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.

var myValue = "<%= ViewData["MyValue"] %>";
alert(myValue);

The alert call is just to show that you have it. Obviously you can use it as you wish.

ま柒月 2024-10-10 15:58:41

您需要在 PartialView 中传递 InstId 并在 PartialView 内部访问它。最好的方法是创建一个 AttributeViewModel(其中包含 InstID)并将其传递到局部视图。

jQuery 可以使用内联代码访问它,如下所示:

var InstID = "<%= Model.InstID %>"
//make the jQuery postback call here using InstID

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 :

var InstID = "<%= Model.InstID %>"
//make the jQuery postback call here using InstID
歌入人心 2024-10-10 15:58:41

试试这个代码

 var fieldid = JSON.parse('@Html.Raw(Json.Encode(ViewData["fieldid"]))');

try this code

 var fieldid = JSON.parse('@Html.Raw(Json.Encode(ViewData["fieldid"]))');
极度宠爱 2024-10-10 15:58:41

我所有的 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文