c# - 如果我的值链接到实例的变量,我应该如何将值传递给 Controller ​​中的验证操作?

标签 c# asp.net-mvc jquery-validate

我才刚刚开始使用 MVC 和 jQuery 验证,所以请多多包涵。我也不知道我的问题的标题应该是什么。 8(

概览

我正在使用 MVC 4 和 jQuery 验证。我的表单正在客户端进行验证。我有一个场景,我的表单上需要有两个非常相似的对象。这是通过具有两个属性的 ModelView 实现的。 ModelView 链接到 View,除远程验证外一切正常。我需要根据对象中的特定值验证字段。除了 Controller 中验证操作的参数外,一切都很好地链接在一起。 在你给我不赞成之前 tsk tsks,我编了下面的代码场景。

代码

Model 类,其中 Name 需要根据 GroupID 的值进行远程验证。本质上,该名称对于该组是唯一的。

public class Colour
{
    [Key]
    public int GroupID {get;set;}

    [Required]
    [Remote("ColourExists", "Validation", AdditionalFields = "GroupID")]
    public string Name {get;set;}
}
ColourExists 操作所在的

验证 Controller 。

public class ValidationController :Controller {
    public JsonResult ColourExists(string name, string groupID) {
        // Add validation here later
        return Json(false, JsonRequestBehavior.AllowGet);
    }
}

ViewController 链接到 ModelView 以便我可以在表单上显示两个单独的实例。通常我需要向用户询问一组的亮色和暗色。 (在你 tsk 之前,请记住,这不是真的)

 public class ColourViewModel {
     public Models.Colour BrightColour { get; set; }
     public Models.Colour DarkColour {get;set;}
 }

生成的 HTML 具有输入字段 BrightColour_NameDarkColour_Name。这些字段具有 data-val-remote-additionalfields=*.Name 属性。在模糊时,它们 GET 正确的操作和 Controller ,但参数为空。预期的参数是 InstanceName.VariableName,例如 BrightColour.NameDarkColour.Name。请求发送如下 Validation/ColourExists?BrightColour.Name=red&BrightColour.GroupID=10

那么,如果我的值链接到实例的变量,我应该如何将值传递给验证 Controller 中的 ColourExists 操作?

编辑

View 如下所示:

@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);
}

但是,在您的情况下您不能这样做,因为您在 ViewModel(而不是 ModelView)中使用了两个相同的实体,并且每个实体都有自己的前缀。因此,绑定(bind)失败。

因此,您必须创建两个单独的 ViewModel:

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; }
}

然后,像这里一样重新定义您的 ColourViewModel:

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);
}

关于c# - 如果我的值链接到实例的变量,我应该如何将值传递给 Controller ​​中的验证操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17551937/

相关文章:

asp.net - 在 ASP.NET MVC 页面生命周期中,我最早什么时候可以访问 SESSION?

c# - .net 强类型 View 模型未设置为对象实例

javascript - 如何在 jQuery 验证中检查元素是否具有类?

jquery-plugins - 使用 JQuery 验证器 (Cakephp) 进行服务器端验证

c# - MSFakes 命名约定是否与通用类相匹配?

c# - 从嵌入式资源中删除带前缀的命名空间

c# - 在 onClick 函数中传递 C# 变量参数

c# - 从一种 PixelFormat 转换为另一种 WPF

c# - 相对于 x86,x64 下 Winforms 程序的启动速度慢 10 倍

JQuery 表单验证和提交脚本冲突