<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET MVC</title><link>http://forums.asp.net/1146.aspx</link><description>Discussions regarding Model-View-Controller (MVC) support in ASP.NET.  &lt;a href="http://forums.asp.net/1215.aspx"&gt;T4MVC subforum&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3273789.aspx</link><pubDate>Fri, 03 Jul 2009 15:52:18 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273789</guid><dc:creator>CodeHobo</dc:creator><author>CodeHobo</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273789.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3273789</wfw:commentRss><description>&lt;p&gt;RuleViolation the class isn&amp;#39;t part of asp.net mvc. You have to define it manually. It&amp;#39;s mentioned in the nerd dinner tutorial that I linked to earlir. Here is the class&lt;/p&gt;&lt;p&gt;&lt;pre name="code" class="c-sharp"&gt;public class RuleViolation {
    public string ErrorMessage { get; private set; }
    public string PropertyName { get; private set; }
 
    public RuleViolation(string errorMessage, string propertyName) {
        ErrorMessage = errorMessage;
        PropertyName = propertyName;
    }
}&lt;/pre&gt;&lt;br /&gt; You can put it in the same folder as the controller, but a better location would be in the same namespace as the repository.&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3273383.aspx</link><pubDate>Fri, 03 Jul 2009 11:22:40 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3273383</guid><dc:creator>Svevarn</dc:creator><author>Svevarn</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3273383.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3273383</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public class RuleValidationException : Exception
    {   
        public IEnumerable&amp;lt;RuleViolation&amp;gt; RuleViolations { get; private set; }   
        public RuleValidationException(string message, IEnumerable&amp;lt;RuleViolation&amp;gt; ruleViolations)   
           : base(message)
        {   
            RuleViolations = ruleViolations;   
        }   
    }  &lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;where shall I put that? If I put it in controller I get:&lt;/p&gt;
&lt;p&gt;Error&amp;nbsp;1&amp;nbsp;The type or namespace name &amp;#39;RuleViolation&amp;#39; could not be found (are you missing a using directive or an assembly reference?)&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3272044.aspx</link><pubDate>Thu, 02 Jul 2009 18:32:50 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3272044</guid><dc:creator>CodeHobo</dc:creator><author>CodeHobo</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3272044.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3272044</wfw:commentRss><description>&lt;p&gt;The error message is just a string so you shouldn&amp;#39;t have any problem reading it out&amp;nbsp; of a resource file.&lt;/p&gt;&lt;p&gt;Take a look at this&lt;/p&gt;&lt;p&gt;&lt;a href="http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Localization.aspx"&gt;http://blog.eworldui.net/post/2008/05/ASPNET-MVC---Localization.aspx&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;and this&lt;/p&gt;&lt;p&gt;&lt;a href="http://developmentalmadness.blogspot.com/2009/05/aspnet-mvc-globalizationlocalization.html"&gt;http://developmentalmadness.blogspot.com/2009/05/aspnet-mvc-globalizationlocalization.html&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3272009.aspx</link><pubDate>Thu, 02 Jul 2009 18:13:20 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3272009</guid><dc:creator>Svevarn</dc:creator><author>Svevarn</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3272009.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3272009</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Ok, thanks, I will look into that.&lt;/p&gt;
&lt;p&gt;Is there some easy way of making the error messages come from a resource file? (read: language-support)&lt;/p&gt;
&lt;p&gt;/S&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3271989.aspx</link><pubDate>Thu, 02 Jul 2009 18:04:31 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3271989</guid><dc:creator>CodeHobo</dc:creator><author>CodeHobo</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3271989.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3271989</wfw:commentRss><description>&lt;p&gt;Take a look at this tutorial. It shows you how to do validation&lt;br /&gt;&lt;a href="http://nerddinnerbook.s3.amazonaws.com/Part3.htm"&gt;http://nerddinnerbook.s3.amazonaws.com/Part3.htm&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;If you follow the example there you could add the validation to your entity object (in thise case BookingObject).&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Another way you can do it is directly in your&amp;nbsp; &lt;span&gt;&lt;span&gt;SaveBookingObject, but this may not be the cleanest approach.&amp;nbsp; You can do something like &lt;pre name="code" class="c-sharp"&gt;public void SaveBookingObject(BookingObject bo) {  
 var violations = ValidateBooking(bo);
  if(violations.Count() &amp;gt; 0){
    throw new RuleViolationException(&amp;quot;error&amp;quot;,violations);
  }

 _repository.SaveBookingObject(bo);  
}  

public IEnumerable&amp;lt;RuleViolation&amp;gt; ValidateBooking(BookingObject bo){
  if (String.IsNullOrEmpty(bo.Property))
        yield return new RuleViolation(&amp;quot;Property is Required&amp;quot;,&amp;quot;Property&amp;quot;);

  yield break;
}&lt;/pre&gt;&lt;br /&gt; &lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3271972.aspx</link><pubDate>Thu, 02 Jul 2009 17:53:54 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3271972</guid><dc:creator>Svevarn</dc:creator><author>Svevarn</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3271972.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3271972</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;At the moment I have no validation in my &amp;quot;service-layer&amp;quot;, method just look like this:&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt; public void SaveBookingObject(BookingObject bo)
        {
            _repository.SaveBookingObject(bo);
        }&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;How shall I add something there and specify which fields are needed?&lt;/p&gt;</description></item><item><title>Re: Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3271953.aspx</link><pubDate>Thu, 02 Jul 2009 17:44:05 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3271953</guid><dc:creator>CodeHobo</dc:creator><author>CodeHobo</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3271953.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3271953</wfw:commentRss><description>&lt;p&gt;1) How are you doing the validation in SaveBookingObject? Depending on the validation scheme you should be able to customize which fields are required in there.&lt;/p&gt;&lt;p&gt;2) The way I list is all violations is to create RuleViolationException that extends Exception and adds and IEnumerable of rule violations like so&lt;/p&gt;&lt;p&gt;&lt;pre name="code" class="c-sharp"&gt; public class RuleValidationException : Exception
    {
        public IEnumerable&amp;lt;RuleViolation&amp;gt; RuleViolations { get; private set; }
        public RuleValidationException(string message, IEnumerable&amp;lt;RuleViolation&amp;gt; ruleViolations)
            : base(message)
        {
            RuleViolations = ruleViolations;
        }
    }&lt;/pre&gt;&lt;br /&gt;In the respoistory I throw a RuleValidationException and put all the violations in there and then catch it&amp;nbsp; and add it to modelstate. (you can do this either in the controller directly or in a service).&lt;br /&gt;&lt;pre name="code" class="c-sharp"&gt;try{
      bs.SaveBookingObject(bookingobject); 
}
catch (RuleValidationException e){
     var ruleViolations = e.RuleViolations;
     foreach (var violation in ruleViolations){
       ModelState.AddError(violation.PropertyName, violation.ErrorMessage);
    }
 }&lt;/pre&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Making it valid, and seeing whats not</title><link>http://forums.asp.net/thread/3271914.aspx</link><pubDate>Thu, 02 Jul 2009 17:19:40 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3271914</guid><dc:creator>Svevarn</dc:creator><author>Svevarn</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3271914.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=1146&amp;PostID=3271914</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m trying to save using the Create action to database. I have made a form that shows some of the fields that you can edit. However it always runs the &amp;quot;catch&amp;quot; when I try to save. I dont want to fill in data to all the field in the database, just some of it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateObject(BookingObject bookingobject)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    bs.SaveBookingObject(bookingobject);
                }
                catch {
                    ModelState.AddModelError(&amp;quot;SomeError&amp;quot;, &amp;quot;errrrrrrrrror&amp;quot;);
                }
            }

            return View(&amp;quot;CreateObject&amp;quot;, new BookingObjectsAdminModel { BookingObject = bookingobject });
        }&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;So my questions:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;1) Can I specify which fields needs to be filled in in the actionmethod?&lt;/p&gt;
&lt;p&gt;2) Can I instead of ModelState.AddError somehow list all &amp;quot;violations&amp;quot;?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks in advance&lt;/p&gt;
&lt;p&gt;/S&lt;/p&gt;</description></item></channel></rss>