如果我的值链接到实例变量,应如何将值传递给Controller中的验证操作? [英] How should I pass values through to a validation action in a Controller if my values are linked to variables of an instance?
问题描述
我才刚刚开始使用MVC和jQuery validate,所以请多多包涵.我也不知道我的问题的标题应该是什么. 8(
I have only just started using MVC and jQuery validate so please bear with me. I also have no idea what the title of my question should be. 8(
概述
我正在将MVC 4与jQuery validate一起使用.我的表格正在客户端验证.我有一个场景,其中两个非常相似的对象需要放在我的表单上.这是通过具有两个属性的ModelView实现的. ModelView链接到视图,除远程验证外,其他所有功能均可用.我需要根据对象中的特定值来验证字段.除了控制器中验证动作的参数外,其他所有东西都很好地链接在一起. 在您拒绝我的同意之前,我编造了以下代码方案.
I am using MVC 4 with jQuery validate. My form is being validated on the client side. I have a scenario where two very alike objects need to be on my form. This has been achieved by means of a ModelView which has two properties. The ModelView is linked to the View and everything works excepting the remote validation. I need to validate a field based on a particular value in the object. Everything is linked together nicely excepting the parameters of the validation action in the controller. Before you give me disapproving tsk tsks, I made up the following code scenario.
代码
Model 类,其中Name
需要根据GroupID
的值进行远程验证.从本质上讲,该名称对于该组是唯一的.
Model class where Name
requires remote validation depending on the value of GroupID
. Essentially, the name is unique to the group.
public class Colour
{
[Key]
public int GroupID {get;set;}
[Required]
[Remote("ColourExists", "Validation", AdditionalFields = "GroupID")]
public string Name {get;set;}
}
验证控制器. 视图和控制器链接到 ModelView ,因此我可以在表单上显示两个单独的实例.通常,我需要向用户询问一组的明暗颜色. (在开始之前,请记住,这不是真实的) The View and Controller is linked to a ModelView so that I can display two separate instances on my form. Typically I need to ask the user for a Bright and a Dark colour for one group. (Before you tsk, remember, this isn't for real) 生成的HTML 具有输入字段 The generated HTML has input fields 如果我的值链接到实例变量,那么应该如何将这些值传递给验证控制器中的 So how should I pass the values through to the 编辑 Edit 视图如下:
通常,在这种情况下,您将在远程验证操作中使用前缀,如下所示: Normally, in this situation you would use a prefix in your remote validation action like here: 但是,您不能这样做,因为您在ViewModel中使用了两个相同的实体(不是ModelView),并且每个实体都有自己的Prefix.因此,绑定失败. But, you can't do that in your case, because you are using two same entities in your ViewModel (not ModelView), and each has its own Prefix. So, the binding fails. 因此,您必须创建两个单独的ViewModel: So, you'll have to create two separate ViewModels: 然后,按如下所示重新定义您的ColourViewModel: Then, redefine your ColourViewModel like here: 然后,创建两个单独的远程验证操作,如下所示: And, then create two separate remote validation actions like this: 这篇关于如果我的值链接到实例变量,应如何将值传递给Controller中的验证操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!public class ValidationController :Controller {
public JsonResult ColourExists(string name, string groupID) {
// Add validation here later
return Json(false, JsonRequestBehavior.AllowGet);
}
}
public class ColourViewModel {
public Models.Colour BrightColour { get; set; }
public Models.Colour DarkColour {get;set;}
}
BrightColour_Name
和DarkColour_Name
.这些字段具有data-val-remote-additionalfields=*.Name
属性.模糊时,它们GET
正确的操作和控制器,但参数为空.预期的参数是InstanceName.VariableName
,例如BrightColour.Name
和DarkColour.Name
.请求发送如下Validation/ColourExists?BrightColour.Name=red&BrightColour.GroupID=10
BrightColour_Name
and DarkColour_Name
. These fields have data-val-remote-additionalfields=*.Name
attributes. On blur they GET
the correct action and controller but the parameters are null. The expected parameters are InstanceName.VariableName
such as BrightColour.Name
and DarkColour.Name
. The request is sent as follows Validation/ColourExists?BrightColour.Name=red&BrightColour.GroupID=10
ColourExists
操作?ColourExists
action in the validation Controller if my values are linked to variables of an instance?@model Colours.ViewModels.ColourViewModel
@using (Html.BeginForm()) {
@Html.LabelFor(model => model.DarkColour.Name)
@Html.EditorFor(model => model.DarkColour.Name)
@Html.HiddenFor(model => model.DarkColour.GroupID)
<input type="submit" value="Save" />
}
推荐答案
public JsonResult ColourExists([Bind(Prefix = "BrightColour")] string name) {
// Add validation here later
return Json(false, JsonRequestBehavior.AllowGet);
}
public class BrightColourViewModel
{
public int GroupID { get; set; }
[Required]
[Remote("BrightColourExists", "Home", AdditionalFields = "GroupID")]
public string Name { get; set; }
}
public class DarkColourViewModel
{
public int GroupID { get; set; }
[Required]
[Remote("DarkColourExists", "Home", AdditionalFields = "GroupID")]
public string Name { get; set; }
}
public class ColourViewModel
{
public BrightColourViewModel BrightColour { get; set; }
public DarkColourViewModel DarkColour { get; set; }
}
public JsonResult BrightColourExists(BrightColourViewModel brightColour)
{
// Call shared code to check if colour exists
return Json(false, JsonRequestBehavior.AllowGet);
}
public JsonResult DarkColourExists(DarkColourViewModel darkColour)
{
// Call shared code to check if colour exists
return Json(false, JsonRequestBehavior.AllowGet);
}