views:

52

answers:

1

In Django/Python, if I had the following model

class Model1:
     id = char field
     name = char field
     creation_time = datetime field

I can create a form (view model) like the following

class Model1Form(Model1):
    exclude = {'id', 'creation_time'}

I then pass it into a view/template and it would ignore id/creation_time

In this case, no validation would be run for id and creation_time. I would set them later in my code and save it.

Is there some way in ASP.NET MVC (using data annotations or whatever else) to exclude a field like that (without using a seperate view model)? I'm using ADO.NET Entities.

+3  A: 
[AcceptVerbs(HttpVerbs.Post)]   
public ActionResult Create([Bind(Exclude="Id")]Product productToCreate)  // <--- 
{   
    if (!ModelState.IsValid)   
        return View();   

    try  
    {   
        _dataModel.AddToProductSet(productToCreate);   
        _dataModel.SaveChanges();   

        return RedirectToAction("Index");   
    }   
    catch  
    {   
        return View();   
    }   
}  
Robert Harvey