0

我在解决方案下有一个解决方案,其中有几个项目称为 DomainModel,我在其中编写我的模型和其他东西,主要是基础设施。

现在我有另一个名为 WebUI 的项目,我在其中做我的 UI(视图、控制器等...)

我想在必须在 WebUI 特定视图中实现的 DomainModel 项目中使用 Remote 属性。

当我在 DomainModel 中使用它时,它给了我一个错误,它无法识别控制器并且它是正确的它无法识别它,因为如果我添加 WebUI 的引用,Vs 开始对我发誓,因为它将是一个循环引用.

如何实施?

这是我为 RemoteValidation 服务的代码控制器

[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class RemoteValidationController : Controller
{
    public JsonResult CheckPassword(string SmsCode)
    {
        return Json(12345, JsonRequestBehavior.AllowGet);
    }
}

//DomainModel项目中的真实实体

public class SmsCustomer
{
    public int CustomerId { get; set; }
    public string Cli { get; set; }
    //this is what i have to validate on server
    public virtual string SmsCode { get; set; }

    public DateTime InsertDate { get; set; }
    public int CustomerDaysChoiceId { get; set; }
    public int CustomerAmountChoiceId { get; set; }

    [Required(ErrorMessage = "error")]
    [StringLength(128, ErrorMessage = "error")]        
    public string SelectedWords { get; set; }
    public SmsCustomerDaysChoice CustomerDaysChoice { get; set; }
    public SmsCustomerAmountChoice CustomerAmountChoice { get; set; }
}

这是我在 WebUI.Models 中使用远程属性扩展它后的实体

 public class Customer : SmsCustomer
    {
        [Required(ErrorMessage = "Error required")]
        [StringLength(9, ErrorMessage = "Error length")]
        [Remote("CheckPassword", "RemoteValidation", ErrorMessage = "Error  remote")]
        public override string SmsCode { get; set; }
    }

这是我的看法

@Html.TextBoxFor(c => c.SmsCode)
//error span
<span class="checkbox-form-error" data-valmsg-for="SmsCode" data-valmsg-replace="true">&nbsp;</span>
4

1 回答 1

1

The remote validation stuff is very specific to the WebUI project.

Because of this, I'd create a View model that inherits from the actual class, and then override the property that needs remote validation. Then you should be able to specify the controller/action for remote validation.

You can also put your validation in a class of its own, like ScottGu demonstrates here: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

(Look down the post, before the last step)

Also take a look at this: Adding DataAnnontations to Generated Partial Classes

于 2011-07-06T09:20:06.763 回答