Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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
C# 在MVC中发送远程属性上的参数_C#_Asp.net Mvc - Fatal编程技术网

C# 在MVC中发送远程属性上的参数

C# 在MVC中发送远程属性上的参数,c#,asp.net-mvc,C#,Asp.net Mvc,是否有一种方法可以使用Remote属性将参数(而不是字段名)从模型类发送到控制器 例如,我有一个模型类: public class SubCategory { [Key] public int ID { get; set; } public string ClassName { get { return "SubCategory"; } } [StringLength(450)] [Remote("IsSubCategory

是否有一种方法可以使用
Remote
属性将参数(而不是字段名)从
模型
类发送到
控制器

例如,我有一个
模型
类:

public class SubCategory
{
    [Key]
    public int ID { get; set; }
    public string ClassName
    {
        get { return "SubCategory"; }
    }
    [StringLength(450)]
    [Remote("IsSubCategoryExist", "Products", AdditionalFields = "ID, ClassName",
            ErrorMessage = "Product name already exists")]
    public virtual string Name { get; set; }
    public virtual string ParentName { get; set; }
}
触发
Remote
时,我需要将此类名
SubCategory
作为参数发送到
Products
控制器中的
IsSubCategoryExist
方法。这能实现吗

更新:

控制器代码:

public async Task<JsonResult> IsSubCategoryExist(string Name, int? ID, string ClassName)
{
      var type = Assembly.GetExecutingAssembly()
                .GetTypes()
                .FirstOrDefault(t => t.Name == ClassName);

      if (type != null)
      {
         DbSet dbSet = _dbContext.Set(type);

         List<object> subCategories;
         if(ID == null)
         {
             subCategories = await dbSet.Where("Name == @0", Name) 
                                        .Take(1).ToListAsync();
         }
         else
         {
             subCategories = await dbSet.Where("Name == @0 and Id != @1", Name,ID)
                                        .Take(1).ToListAsync();
         }

         if(subCategories.Count > 0)
         {
            return Json(false,JsonRequestBehavior.AllowGet);
         }
         return Json(true,JsonRequestBehavior.AllowGet);
     }
     else
     {
        throw new Exception("Table name does not exist with the provided name");
     }
}
公共异步任务IsSubCategoryExist(字符串名称、int?ID、字符串类名称)
{
var type=Assembly.getExecutionGassembly()
.GetTypes()
.FirstOrDefault(t=>t.Name==ClassName);
if(type!=null)
{
DbSet DbSet=_dbContext.Set(类型);
列出子类别;
if(ID==null)
{
子类别=等待dbSet.Where(“Name=@0”,Name)
.Take(1.ToListAsync();
}
其他的
{
子类别=等待dbSet.Where(“名称==@0和Id!=@1”,名称,Id)
.Take(1.ToListAsync();
}
如果(subCategories.Count>0)
{
返回Json(false,JsonRequestBehavior.AllowGet);
}
返回Json(true,JsonRequestBehavior.AllowGet);
}
其他的
{
抛出新异常(“表名与提供的名称不存在”);
}
}

我想您可以使用静态值向模型中添加一个字段,然后将其添加到AdditionalFields中:

public string ClassName { get {return "SubCategory";} }

[Remote("IsSubCategoryExist", "Products", AdditionalFields = "ID, ClassName",
        ErrorMessage = "Product name already exists")]
public virtual string Name { get; set; }

除了做以下工作外:

public string ClassName { get {return "SubCategory";} }

[Remote("IsSubCategoryExist", "Products", AdditionalFields = "ID, ClassName",
        ErrorMessage = "Product name already exists")]
public string Name { get; set; }
您必须按如下形式放置一个隐藏字段,否则它将给出
未定义的类名
错误:

Html.HiddenFor(x => x.ClassName)

抱歉
ClassName
在调试模式下在控制器中显示为“未定义”。我已经更新了上面的
Controller
代码。@这是因为在您的视图中没有名为
ClassName
的输入字段。您可以在窗体中为ClassName创建一个隐藏的输入字段。