Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用MVC4进行远程jQuery验证,动态验证从loc开始的所有输入?_Jquery_Asp.net Mvc_Asp.net Mvc 4_Server Side Validation - Fatal编程技术网

使用MVC4进行远程jQuery验证,动态验证从loc开始的所有输入?

使用MVC4进行远程jQuery验证,动态验证从loc开始的所有输入?,jquery,asp.net-mvc,asp.net-mvc-4,server-side-validation,Jquery,Asp.net Mvc,Asp.net Mvc 4,Server Side Validation,说明: 我在MVC4应用程序中有一个简单的表单,它有5个名为loc1-5的文本框和一个提交按钮。该应用程序在文本框loc1-5中最多占用5个地址,并使用bing geocode服务和jQuery对这些地址进行处理,并创建带有方向的地图 问题是,在继续之前,我需要验证loc1-5文本框,以确保它们是有效地址,并决定最好的方法是使用jQuery.validate远程调用MVC控制器函数,该函数可以使用我的预构建函数检查有效地址 现在,我确实提出了一个有效的解决方案来验证这些字段,但我迫切需要使其更具

说明:

我在MVC4应用程序中有一个简单的表单,它有5个名为loc1-5的文本框和一个提交按钮。该应用程序在文本框loc1-5中最多占用5个地址,并使用bing geocode服务和jQuery对这些地址进行处理,并创建带有方向的地图

问题是,在继续之前,我需要验证loc1-5文本框,以确保它们是有效地址,并决定最好的方法是使用jQuery.validate远程调用MVC控制器函数,该函数可以使用我的预构建函数检查有效地址

现在,我确实提出了一个有效的解决方案来验证这些字段,但我迫切需要使其更具动态性,以便将来可以用最小的努力添加更多的文本框。理想情况下,我希望逻辑能够对以“loc”开头的所有输入进行验证

工作溶液非常脏:

MVC视图中的简单表单

<form action="/Home/ViewResult" method="post" id="ViewResult" name="ViewResult">
<fieldset>
<legend>Enter Route</legend>
<p>
Address 1 (Start & End):
</p>
<p>
<input type="text" id="loc1" name="loc1" value='' />
</p>
<p>
Address 2:
</p>
<p>
<input type="text" id="loc2" name="loc2" value='' />
</p>
<p>
Address 3:
</p>
<p>
<input type="text" id="loc3" name="loc3" value='' />
</p>
<p>
Address 4:
</p>
<p>
<input type="text" id="loc4" name="loc4" value='' />
</p>
<p>
Address 5:
</p>
<p>
<input type="text" id="loc5" name="loc5" value='' />
</p>
<p>
<input type="submit" value="Route"/>
</p>
</fieldset>
</form>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
MVC视图中的jQuery验证代码

<form action="/Home/ViewResult" method="post" id="ViewResult" name="ViewResult">
<fieldset>
<legend>Enter Route</legend>
<p>
Address 1 (Start & End):
</p>
<p>
<input type="text" id="loc1" name="loc1" value='' />
</p>
<p>
Address 2:
</p>
<p>
<input type="text" id="loc2" name="loc2" value='' />
</p>
<p>
Address 3:
</p>
<p>
<input type="text" id="loc3" name="loc3" value='' />
</p>
<p>
Address 4:
</p>
<p>
<input type="text" id="loc4" name="loc4" value='' />
</p>
<p>
Address 5:
</p>
<p>
<input type="text" id="loc5" name="loc5" value='' />
</p>
<p>
<input type="submit" value="Route"/>
</p>
</fieldset>
</form>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
遥控器引用的家庭控制器中的功能

// Function to check for a valid address public Boolean IsValidAddress(string location) { // If it is not blank if (location != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(location); // If no waypoint returned, return false if (waypoint == null) { return false; } } return true; } public JsonResult isValidAddress1(string loc1) // Parameter must be textbox name { if (!IsValidAddress(loc1)) { return new JsonResult { Data = false }; } return new JsonResult { Data = true }; } public JsonResult isValidAddress2(string loc2) // Parameter must be textbox name { if (!IsValidAddress(loc2)) { return new JsonResult { Data = false }; } return new JsonResult { Data = true }; } public JsonResult isValidAddress3(string loc3) // Parameter must be textbox name { if (!IsValidAddress(loc3)) { return new JsonResult { Data = false }; } return new JsonResult { Data = true }; } public JsonResult isValidAddress4(string loc4) // Parameter must be textbox name { if (!IsValidAddress(loc4)) { return new JsonResult { Data = false }; } return new JsonResult { Data = true }; } public JsonResult isValidAddress5(string loc5) // Parameter must be textbox name { if (!IsValidAddress(loc5)) { return new JsonResult { Data = false }; } return new JsonResult { Data = true }; } 问题:

同样,这是可行的,但它非常脏,根本不是动态的

基本上我有两个问题

如何编写jQuery速记来为所有以loc开头的文本框创建验证规则? 据我所知,处理远程调用的MVC控制器函数必须将文本框的名称传递给它。那么,如何让一个MVC控制器函数处理对它的多个远程调用呢? 我对jQuery不是很在行,但我真正想要的是这样的东西,这样我以后可以用最少的努力添加更多的文本框:

和家庭控制器的功能

// Function to check for a valid address public JsonResult IsValidAddress(string loc) // loc variable connect to dynamic textbox names? { // If it is not blank if (loc != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(loc); // If no waypoint returned, return false if (waypoint == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } 最后请注意,我没有能力更改MVC模型。我见过许多类似的解决方案,它们直接在MVC模型中编写验证规则和远程调用,但我不能这样做

欢迎提出任何改进建议,并提前感谢您的回复


请尝试告诉我哪里出了问题,或者我想要的是否可能。因此,我能够通过在规则声明中使用远程调用的数据属性来定义一个名为“address”的新参数来解决问题的第二部分,该参数随后在MVC控制器函数中作为参数引用

以下是家庭控制器中MVC控制器功能的更新代码:

// Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } // Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } 对于jQuery,有两种方法都可以使用

首先使用我之前使用的格式:

或使用.rulesadd方法:


因此,我能够通过在规则声明中使用远程调用的data属性来定义一个名为“address”的新参数来解决问题的第二部分,该参数随后在MVC控制器函数中作为参数引用

以下是家庭控制器中MVC控制器功能的更新代码:

// Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } // Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } 对于jQuery,有两种方法都可以使用

首先使用我之前使用的格式:

或使用.rulesadd方法:

所以我想出来了

最后工作解决方案:

MVC家庭控制器中的功能:

// Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } // Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } 视图中的jQuery函数:

我花了很多时间想弄明白这一点,我想我会和大家分享。

所以我想明白了

最后工作解决方案:

MVC家庭控制器中的功能:

// Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } // Function to check for a valid address // Note: address variable parameter connects to data attribute in remote call public JsonResult IsValidAddress(string address) { // If it is not blank if (address != "") { // Attempt to get the waypoint Waypoint waypoint = getWaypoint(address); // If no waypoint returned, return false if (waypoint.Location == null) { return new JsonResult { Data = false }; } } return new JsonResult { Data = true }; } 视图中的jQuery函数:


我花了很多时间想弄明白这一点,我想我会和大家分享。

欢迎来到so,Paulie。你回答了自己的问题,这是一件好事。但是,请不要在回答中提出后续问题。编辑原始问题或发布第二个独立问题。欢迎使用SO,Paulie。你回答了自己的问题,这是一件好事。但是,请不要在回答中提出后续问题。编辑原始问题或发布第二个单独的问题。