Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Servicestack validation

 REST Web Services Framework" <servicestack@googlegroups.com>
Date: Fri, 9 Nov 2012 21:53:12 -0800 (PST)
From: Ermias <erm...@gmail.com>
To: servicestack@googlegroups.com
Message-Id: <f140c0e7-e6b1-4d3f-98c6-a90d571a9279@googlegroups.com>
Subject: Servicestack validation
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_218_3830207.1352526792378"

------=_Part_218_3830207.1352526792378
Content-Type: multipart/alternative; 
	boundary="----=_Part_219_16897431.1352526792379"

------=_Part_219_16897431.1352526792379
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

I have a model

[Validator(typeof(ContractValidator))]
[Route("/contracts", "POST")]
public class Contract
{
public int ContractID{get; set;}
public string ContractNumber{get;set;}
}

I also have a validation class for the model above.

 public class ContractValidator : AbstractValidator<Contract>
    {
        public ContractValidator()
        {
            RuleFor(x => x.ContractNumber).Length(0, 10);
        }
    }

I also have a class to map a 'GET' request:

[Route("/contracts/{ContractID}", "GET")]
public class GetContract
  {
     public int ContractID { get; set; }
  }

...and a service:


public class ContractService : ServiceStack.ServiceInterface.Service
{
public object Get(GetContract request)
        {
            IContractRepository repository = new ContractRepository();

            return repository.GetById(request.ContractID);
            
        }

public object Post(Contract request)
        {
            IContractRepository repository = new ContractRepository();
            repository.Save(request);
            //return request; 
        }

}

Now in my Razor view to edit a contract object I have

@inherits ViewPage<Contract>
.
.
.

But the problem is I'm getting the following error when I try to save and 
there is a model validation error:

Unable to cast object of type 
'ServiceStack.ServiceInterface.ServiceModel.ErrorResponse' to type 
'DTO.Contract'

Can someone please let me know how to handle this?

Thanks,

Ermias.

------=_Part_219_16897431.1352526792379
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<div>I have a model</div><div><br></div><div>[Validator(typeof(ContractVali=
dator))]</div><div>[Route("/contracts", "POST")]</div><div>public class Con=
tract</div><div>{</div><div><span class=3D"Apple-tab-span" style=3D"white-s=
pace:pre">=09</span>public int ContractID{get; set;}</div><div><span class=
=3D"Apple-tab-span" style=3D"white-space:pre">=09</span>public string Contr=
actNumber{get;set;}</div><div>}</div><div><br></div><div>I also have a vali=
dation class for the model above.</div><div><br></div><div>&nbsp;public cla=
ss ContractValidator : AbstractValidator&lt;Contract&gt;</div><div>&nbsp; &=
nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; public ContractValidator()</d=
iv><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; &nbsp; RuleFor(x =3D&gt; x.ContractNumber).Length(0, 10);</div><div=
>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div><br></di=
v><div>I also have a class to map a 'GET' request:</div><div><br></div><div=
>[Route("/contracts/{ContractID}", "GET")]</div><div>public class GetContra=
ct</div><div>&nbsp; {</div><div>&nbsp; &nbsp; &nbsp;public int ContractID {=
 get; set; }</div><div>&nbsp; }</div><div><br></div><div>...and a service:<=
/div><div><br></div><div><br></div><div>public class ContractService : Serv=
iceStack.ServiceInterface.Service</div><div>{</div><div><span class=3D"Appl=
e-tab-span" style=3D"white-space:pre">=09</span>public object Get(GetContra=
ct request)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; IContractRepository repository =3D new Contrac=
tRepository();</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; return repository.GetById(request.ContractID);</div><div>&nbsp; &nbs=
p; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; =
}</div><div><br></div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">=09</span>public object Post(Contract request)</div><div>&nbsp; &n=
bsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IC=
ontractRepository repository =3D new ContractRepository();</div><div>&nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repository.Save(request);</div><div>&nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return request;&nbsp;</div><div>&n=
bsp; &nbsp; &nbsp; &nbsp; }</div><div><br></div><div>}</div><div><br></div>=
<div>Now in my Razor view to edit a contract object I have</div><div><br></=
div><div>@inherits ViewPage&lt;Contract&gt;</div><div>.</div><div>.</div><d=
iv>.</div><div><br></div><div>But the problem is I'm getting the following =
error when I try to save and there is a model validation error:</div><div><=
br></div><div>Unable to cast object of type 'ServiceStack.ServiceInterface.=
ServiceModel.ErrorResponse' to type 'DTO.Contract'</div><div><br></div><div=
>Can someone please let me know how to handle this?</div><div><br></div><di=
v>Thanks,</div><div><br></div><div>Ermias.</div>
------=_Part_219_16897431.1352526792379--

------=_Part_218_3830207.1352526792378--