0

I have one project in mvc.

i have employee model file with me that's is an dll file code

which defines all the fields of my form , i want to change the [DataMember(IsRequired = false)] for LName field

but as i have model file in term of dll i could not able to change it in .cs file

is there any other way?

code in .cs

[DataMember(IsRequired = true)]
[Display(Name = "Lname")]
[Metadata(MetadataId = "142C8DF5-0546-4C4A-A935-CA39D5AF0E2F", Order = 10, IsSearchable = true, IsVisible = true, IsReadonly = false, IsNullable = false, HasDefaultValue = false, DefaultValue = "")]
[Required(ErrorMessage = "Please enter Last Name")]
public double LName { get; set; }

code in .cshtml

<tr>
<td valign="top" class="nd_nor_ftd">Last Name
<span class="mand">*</span>
</td>
<td>
@Html.TextBoxFor(t => t.LName, new { @class = "smallTxtEntry" })
</tr>

In simple words I want to change that Isrequired field to false which is given as true? Or it should not give any validation error for this field help me

tereško
  • 58,060
  • 25
  • 98
  • 150
Neo
  • 15,491
  • 59
  • 215
  • 405
  • 1
    I think Conditional validation is what you need. – Brij Aug 27 '13 at 08:52
  • please help me in this m new bie – Neo Aug 27 '13 at 09:07
  • Check this [post](http://stackoverflow.com/questions/5630424/asp-net-mvc-disable-client-side-validation-at-per-field-level), it might help. – Nilesh Aug 27 '13 at 09:15
  • 1
    Make a new class say EmployeeModelView with same members as Employee class. Apply the Data Annotations on it as per your requirement. Use this class on View. If all goes well, copy the values from this new class to your Employee object. :) – Brij Aug 27 '13 at 09:52
  • Instead is there any client side property like "data-val" = false or anything else? – Neo Aug 27 '13 at 09:59

2 Answers2

1

@{ Html.EnableClientValidation(false); } @Html.TextBoxFor(m => m.Lname, new { @class = "k-textbox" }) @{ Html.EnableClientValidation(true); }

Neo
  • 15,491
  • 59
  • 215
  • 405
0

If the Employee class is not marked as sealed you can derive from it and hide the LName property of the original class.

public class MyEmployee : Employee
{
    [DataMember(IsRequired = false)]
    [Display(Name = "Lname")]
    [Metadata(MetadataId = "142C8DF5-0546-4C4A-A935-CA39D5AF0E2F", Order = 10, IsSearchable = true, IsVisible = true, IsReadonly = false, IsNullable = false, HasDefaultValue = false, DefaultValue = "")]
    [Required(ErrorMessage = "Please enter Last Name")]
    public new double LName { get; set; }
} 
Stefan P.
  • 9,489
  • 6
  • 29
  • 43
  • it is an sealed class :( is there any other way to do ? my only requirment is to do that property IsRequired = false OR it should not give any validation error? – Neo Aug 27 '13 at 09:07