I have a page that has a partial view withing it.
我有一个部分视图的页面。
MyMainView.cshtml
<div>
@Html.Partial("MyPartial")
</div>
@section pagespecific {
<script type='text/javascript'>
$(document).ready(function(){
console.log('Value is: ' + $('#MyModelField').val());
});
</script>
}
MyPartial.cshtml
<div>
@Html.HiddenFor(x => x.MyModelField)
</div>
The value of MyModelField
in the model is True
, however, I get the following output into the browser console:
模型中MyModelField的值为True,但是,我在浏览器控制台中获得以下输出:
Value is: undefined
Why isn't this picking up the field? Shouldn't the partial view have been added to the DOM before this fires?
为什么这不是这个领域?在此触发之前,部分视图是否应该已添加到DOM中?
If I change the $(document).ready()
to $(window).load()
it works as expected.
如果我将$(document).ready()更改为$(window).load(),它将按预期工作。
How can I achieve the desired behavior?
我怎样才能达到理想的行为?
2 个解决方案
#1
3
Yes it is true, the reason is whenever your dom is ready .ready
is fired. So your page is loaded first and it fires the jquery .ready
function() , while the sequentially execution partial view also execute at server side.
是的,这是真的,原因是你的dom准备就绪。已经被解雇了。因此,首先加载页面并触发jquery .ready函数(),而顺序执行的局部视图也在服务器端执行。
Both are different in the usability.
两者的可用性都不同。
To stop or you want to execute after some time.
停止或想要在一段时间后执行。
- Either you can do this in partial view
您可以在局部视图中执行此操作
or
-
Give the delay time of your duration
给出持续时间的延迟时间
$(document).ready(function(){ console.log('Value is: ' + $('#MyModelField').val()); setTimeout("$('#buttonid').click()", DelayDuration); });
//DelayDuration means give milisecond value, 1000 milisecond = 1 sec
// DelayDuration意味着给出毫秒值,1000毫秒= 1秒
The same question asked here
这里问同样的问题
JQuery events don't work with ASP.NET MVC Partial Views
JQuery事件不适用于ASP.NET MVC部分视图
Is it possible to kick off a javascript function after a partial view renders in MVC Asp.net?
在MVC Asp.net中呈现部分视图后,是否可以启动javascript函数?
#2
0
$(document).ready
doesn't fire before partial is rendered because partial view is rendered on server-side. You're using wrong ID in jquery because @Html.HiddenFor(x => x.MyModelField)
won't create element with ID="MyModelField". Element will have ID with prefix which is dependent on you model class name.
$(document).ready在部分呈现之前不会触发,因为部分视图是在服务器端呈现的。你在jquery中使用了错误的ID,因为@ Html.HiddenFor(x => x.MyModelField)不会创建ID =“MyModelField”的元素。元素将具有带前缀的ID,该ID取决于您的模型类名称。
Inspect element's id in browser tools (F12) and determine the real ID.
在浏览器工具中检查元素的ID(F12)并确定真实ID。
Example: If you model name is User
class. The generated id will look like User_MyModelField
示例:如果您的型号名称是User类。生成的ID将类似于User_MyModelField
#1
3
Yes it is true, the reason is whenever your dom is ready .ready
is fired. So your page is loaded first and it fires the jquery .ready
function() , while the sequentially execution partial view also execute at server side.
是的,这是真的,原因是你的dom准备就绪。已经被解雇了。因此,首先加载页面并触发jquery .ready函数(),而顺序执行的局部视图也在服务器端执行。
Both are different in the usability.
两者的可用性都不同。
To stop or you want to execute after some time.
停止或想要在一段时间后执行。
- Either you can do this in partial view
您可以在局部视图中执行此操作
or
-
Give the delay time of your duration
给出持续时间的延迟时间
$(document).ready(function(){ console.log('Value is: ' + $('#MyModelField').val()); setTimeout("$('#buttonid').click()", DelayDuration); });
//DelayDuration means give milisecond value, 1000 milisecond = 1 sec
// DelayDuration意味着给出毫秒值,1000毫秒= 1秒
The same question asked here
这里问同样的问题
JQuery events don't work with ASP.NET MVC Partial Views
JQuery事件不适用于ASP.NET MVC部分视图
Is it possible to kick off a javascript function after a partial view renders in MVC Asp.net?
在MVC Asp.net中呈现部分视图后,是否可以启动javascript函数?
#2
0
$(document).ready
doesn't fire before partial is rendered because partial view is rendered on server-side. You're using wrong ID in jquery because @Html.HiddenFor(x => x.MyModelField)
won't create element with ID="MyModelField". Element will have ID with prefix which is dependent on you model class name.
$(document).ready在部分呈现之前不会触发,因为部分视图是在服务器端呈现的。你在jquery中使用了错误的ID,因为@ Html.HiddenFor(x => x.MyModelField)不会创建ID =“MyModelField”的元素。元素将具有带前缀的ID,该ID取决于您的模型类名称。
Inspect element's id in browser tools (F12) and determine the real ID.
在浏览器工具中检查元素的ID(F12)并确定真实ID。
Example: If you model name is User
class. The generated id will look like User_MyModelField
示例:如果您的型号名称是User类。生成的ID将类似于User_MyModelField