0

I have a Solution under the solution there is few Projects one of the called DomainModel, in which i write my models and other stuff mainly infrastructure.

Now i have another project called WebUI in which i do my UI (Views, Controllers , etc...)

I want to use Remote attribute in DomainModel project which must implemented in WebUI certain view.

When i use it in DomainModel it's gives me an error, that it does not recognize the Controller and it's correct it does not recognize it because the if I add the reference of WebUI the Vs begin to swear at me because it will be a circular reference.

How to implement this?

this is my code Controller that serves the RemoteValidation

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

//The real entity in DomainModel project

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

this is my entity after i extend it with the remote attr in 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; }
    }

this is my view

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

1 Answers1

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

Community
  • 1
  • 1
MartinHN
  • 19,542
  • 19
  • 89
  • 131
  • can you please demonstrait it? – hackp0int Jul 06 '11 at 09:23
  • I added links to a solution that is a bit different. Those use separate classes to contain the validation annotations. – MartinHN Jul 06 '11 at 09:30
  • Basic tutorial about remote validation can be found @ http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx – Narendra V Jul 06 '11 at 13:27
  • i have found my mistake i have created a basic request, but now i get strange error from jquery (range error validation) and i don't use it at all. The fix i was needed to, is just to wrap the wanted inputs including the error-place-holder within a form. – hackp0int Jul 06 '11 at 13:30