2010-02-14 104 views
2

在Django中/ Python的,如果我有以下模型ASP.NET視圖模型替代

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

我可以創建一個形式(視圖模型)像下面

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

我然後將其傳遞到一個視圖/模板,它會忽略id/creation_time

在這種情況下,不會爲id和creation_time運行驗證。我會稍後在我的代碼中設置它們並保存它。

在ASP.NET MVC(使用數據註釋或其他任何)來排除一個像這樣的字段(不使用單獨的視圖模型)的方式嗎?我正在使用ADO.NET實體。

+0

這樣 - 您的視圖模型是緊耦合與域模型,你失去它的價值。 – 2010-02-16 10:36:05

回答

3
[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(); 
    } 
}