Reputation: 21
I am new to .Net MVC.
Currently due to server specification, i am using MVC 4 and Visual Studio 2012 for Web.
I am using entity framework for CRUD operation.
My problem is, i am a bit confused with how to specify different validation rules for create and edit operation:
My class is something like this: public class Outlet { [Key] public int outletID { get; set; }
[Required]
[StringLength(10)]
public string outletCode { get; set; }
[Required]
[StringLength(100)]
public string outletName { get; set; }
[Required]
[StringLength(50)]
public string outletAreaManager { get; set; }
[Required]
[StringLength(200)]
public string outletAddress { get; set; }
[StringLength(20)]
public string outletUnitNo { get; set; }
[Required]
[StringLength(6)]
public string outletPostalCode { get; set; }
[StringLength(10)]
public string outletPhoneNo { get; set; }
}
When the user create new outlet, they are able to key in all the field, but when do want to edit the outlet, some field like OutletCode, outletAreaManager is uneditable and not displayed in the form and due to some security reason, i couldn't store it in the hiddenfield, what is the best way to do it? I means how to code it when updating to database? if i use ModelState.isValid, it will trigger the required attribute for outletCode and outletAreaManager.
Thanks in advance.
Upvotes: 1
Views: 179
Reputation: 21
sorry for make you all confuse.
Actually what i means is partial updating, so i need to execute form validation only for partial field, and i solve my problem using TryUpdateModel.
Thanks and cheers all
Upvotes: 0
Reputation: 2695
You say you are using EF for the database. The class above looks like a View Model class. I assume you are mapping between your entity class and this view model class.
In order to accomodate different rules you should create two different classes. To avoid duplicae code where the rules are the same between them create a base class then two dervied classes that implement the differences.
Something like this perhaps:
public class OutletBase {
[Key]
public int outletID { get; set; }
[Required]
[StringLength(100)]
public string outletName { get; set; }
[Required]
[StringLength(200)]
public string outletAddress { get; set; }
[StringLength(20)]
public string outletUnitNo { get; set; }
[Required]
[StringLength(6)]
public string outletPostalCode { get; set; }
[StringLength(10)]
public string outletPhoneNo { get; set; }
}
public class OutledCreate : OutletBase
{
[Required]
[StringLength(10)]
public string outletCode { get; set; }
[Required]
[StringLength(50)]
public string outletAreaManager { get; set; }
}
public class OutletEdit : OutletBase {
}
Upvotes: 1
Reputation: 3153
Well you can store them in a hidden field, you just can store them in a disabled field. That's very trusting validation though. You need to split your models. This is often why people create ViewModels for MVC to use and then map the values from the view model into the entity model. If you did that then when you fetched the record for update you simply would not update the values that were not changeable.
Upvotes: 0