MVC 中的 Javascript onclick _dopostback 不回发
NET MVC 尝试使用 javascript 进行确认后回发,这是按钮:
<input type="submit" id ="RemoveStatus" value="Remove Status" name="button" onclick="return CheckRemove();"/>
这是我的 CheckRemove() js 函数中的 javascript:
var button1 = document.getElementById("RemoveStatus");
if (confirm("Are you sure you want to remove status?") == true)
{
button1.disabled = true;
button1.value = "Removing status...";
__doPostBack('RemoveStatus', '');
return true;
}
else
{
return false;
}
但由于某种原因,我在 __doPostBack 位处收到对象预期错误,我已经明确设置了 id ,button1在调试中也被填充,我尝试将button1.id和button1也传递到__doPostBack调用中,但它不会回发并一直说预期对象,有什么想法吗?
NET MVC to try to postback after confirmation using javascript, this is the button:
<input type="submit" id ="RemoveStatus" value="Remove Status" name="button" onclick="return CheckRemove();"/>
This is my javascript in my CheckRemove() js function:
var button1 = document.getElementById("RemoveStatus");
if (confirm("Are you sure you want to remove status?") == true)
{
button1.disabled = true;
button1.value = "Removing status...";
__doPostBack('RemoveStatus', '');
return true;
}
else
{
return false;
}
But for some reason I get an object expected error at the __doPostBack bit, I've clearly set the id, button1 gets populated too in debug, i'v tried passing button1.id and button1 too into the __doPostBack call but it wont postback and keeps saying object expected, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVC 中没有像 Web 表单中那样的回发概念。只需使用
nameoftheform.submit();
即可。http://www.javascript-coder.com/javascript- form/javascript-form-submit.phtml
您可以使用以下内容在表单标记中生成 id:
并在脚本中使用:
就我个人而言,我是 jQuery 的粉丝,因此最后一个可以重写为:
You don't have the concept of postbacks in MVC like you had in webforms. Simply use
nameoftheform.submit();
instead.http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
You can use the following to generate the id in the form tag:
and in the script use:
Personally I'm a fan of jQuery so that last one could be rewritten like:
尝试像这样进行jquery提交,
它对我有用
try doing jquery submit like this
it worked with me
使用Jquery提交功能。
可以找到一个很好的示例 这里
不要忘记执行:return:false;否则该表格将被发布两次:
一次来自 jquery,一次来自表单中的按钮调用。
另一个不错的功能是,在 MVC 操作(在控制器中)中,如果您使用模型,则可以捕获模型。如果您将模型绑定到文本字段等,您将获得更新的模型,因此您不必解析表单集合。
我从我的一个项目中得到了一些代码。
我有一个存储在数据库中的页面对象。
当我创建视图时,我选择强类型 ->页面对象
这给了我:
我将我的模型绑定到文本框:
在我的控制器中,我这样做:
我在文本字段中放入的所有内容都绑定到我在控制器中检索并存储的模型
Use Jquery submit function.
A nice example can be found here
Don't forget to do a : return:false; otherwise the form will be post twice:
once from the jquery, and once from the button call in the form.
Another nice feature is that in your MVC Action (in your controller) you can catch your Model if you use one. If you bind your model to your textfields etc, you get an updated model, so you don't have to parse your formcollection.
I got some code from a project of mine.
I have a page object that is stored in db.
When I create my view I choose strongly typed-> page object
which gives me:
I bind my model to textboxes:
in my controller I do this:
everything I put in the textfields is bound to the model which I retrieve in the controller and store