I have a problem with my MVC 4 Application. I have a Model with code as follows:
public class MyModel
{
[Required]
[Display(Name = "Code")]
public string Code { get; set; }
[Display(Name = "Type")]
[DisplayFormat(NullDisplayText = "")]
public string Type { get; set; }
}
The Code field in the Model represents a non-nullable type from the database while the Type field accepts null values. Now my problem comes when I try to load items from the Model and into my View using my Controller. Please see the following Controller code:
public ActionResult Display(string Code)
{
MyManager manage= new MyManager();
Model model= new Model();
model = manage.GetItem(Code);
MyModel obj = new MyModel();
obj.Code = model.Code.Trim();
obj.Type = model.Type.Trim();
return View(obj);
}
Basically the GetItems method comes from my manager which accepts the Code and returns data of type Model (which is the name of the table in my database).
The problem points at this line: obj.Type = model.Type.Trim(); whenever null values coming from the database occurs.
Can someone please point me to an effective way on how to handle this problem?
You could try to check the database return before to copy to ViewModel object.