简体   繁体   English

即使在函数中将returnValue设置为False后,也将执行控制器动作

[英]controller action being executed even after setting returnValue to False in function

I have a form with one textbox and one button. 我有一个带有一个文本框和一个按钮的表单。 Here I need to validate if users are not entering invalid data in the textbox so I have a function in site.masters "head" which is called "onclick" event. 在这里,我需要验证用户是否未在文本框中输入无效数据,因此我在site.masters“ head”中具有一个称为“ onclick”事件的函数。 The issue is, even after setting the "returnValue" variable to "False" the controller action is being executed and I get the following error: 问题是,即使将“ returnValue”变量设置为“ False”,也会执行控制器操作,并且出现以下错误:

Server Error in '/' Application. “ /”应用程序中的服务器错误。

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.int32' for method 'System.Web.Mvc.ActionResult AddStudent(System.DateTime)' in 'Student.Controllers.HomeController'. 参数字典包含“ Student.Controllers.HomeController”中方法“ System.Web.Mvc.ActionResult AddStudent(System.DateTime)”的非空类型“ System.int32”的参数“ id”的空条目。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters 参数名称:参数

Controller Action: 控制器动作:

public ActionResult AddStudent(int id)
        {
            StudentDataContext student = new StudentDataContext ();

            var std = student.Students.FirstOrDefault(s => s.StudentId == id);

                        std = new Models.Student() { StudentId = id};
                        student.Students.InsertOnSubmit(std);
                        student.SubmitChanges();
                        TempData["Message"] = "Student " + id + " is added";
                        return RedirectToAction("Index", TempData["Message"]);

       }

Here is my javascript code from Site.Master: 这是我来自Site.Master的javascript代码:

<script language="javascript">

    function verifyInput() {
        var returnValue = true;
        if (document.getElementById('studentId').length != 10) {
        returnValue = false;
        alert("please enter a valid id")
        Form1.studentId.value = "";            
                }
        return returnValue;            
    }

</script>

Here is my form code from my view: 这是我的表单代码:

<form id="Form1" method="get" action="/Home/AddStudent/" runat="server">
    <label for="id">
        <br /><br /> Student ID:
    </label>
        <input type="text" name="studentId" id="studentId" maxlength=10/>
        <input type="submit" value="Add Student" onclick="verifyInput()"/>
</form>

How do I resolve this? 我该如何解决?

Thanks in advance 提前致谢

You need to return returnValue. 您需要return returnValue。 Right now you're setting it to false, but not doing anything with it. 现在,您将其设置为false,但不对其进行任何操作。

Update your onclick event to: 将您的onclick事件更新为:

onclick="return verifyInput();"

or 要么

onclick="verifyInput"

Also, for form validation, you should be checking that on submit, instead of on click. 另外,要进行表单验证,您应该在提交时(而不是单击)进行检查。 That way you capture the miscellaneous times a form submits, such as when they hit the enter button. 这样,您可以捕获表单提交的其他时间,例如当它们按下Enter按钮时。

It would be something similar to: 它将类似于:

<form ... onsubmit="return verifyInput();">

This problem has something more to do with your Routes. 此问题与您的路线有关。 Does your default route, or the one for this action, look something like this? 您的默认路由或执行此操作的路由看起来像这样吗?

new { controller = "Home", action = "Index", id = UrlParameter.Optional }

If not, you will need to append the studentId to the end of your from action before submitting it. 如果不是,您将需要在提交studentId之前将其添加到from动作的末尾。

maybe something like: 也许像这样:

if (document.getElementById('studentId').length != 10) {
  ...false...
} else {
   document.getElementById('Form1').action = document.getElementById('Form1').action = document.getElementById('studentId').value;
}

Also, it may help to set maxlength as "10" instead of 10, wrapping it with quotes. 同样,将maxlength设置为“ 10”而不是10可能会有所帮助,并用引号将其引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery:控制器功能无法使用ajax正确执行 - jQuery: Controller function not being executed correctly with ajax 当执行jQuery的toggleClass()函数时,ASP.NET MVC控制器的操作再次执行 - ASP.NET MVC controller's action is executed again when jQuery's toggleClass() function is executed 即使在错误循环中也执行c# - c# executed even when in false loop Unity-如果条件为false时也执行块 - Unity - if block is executed even when condition is false 在操作结果中设置后,控制器中的private成员返回null - private member in controller returns null after being set in action result 为什么当我传递(按值)字典时会生成$ ReturnValue1 <char, stack<int> &gt;到一个功能 - Why is $ReturnValue1 being generated when i pass (by value) a dictionary<char, stack<int>> to a function 连.net核心controller第一行都没执行 - Even the first line of .net core controller is not executed ObjectDisposedException 即使在将客户端处理程序设置为 false 之后,我也不会处理和使用同一个实例 - ObjectDisposedException even after setting client handler to false so I'm not disposing and using the same instance 查看将从路线执行哪个控制器和操作? - See which controller and action will be executed from a route? 控制器针对单个请求执行两次 - Controller is being executed twice for single request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM