开发者

ASP.NET ViewModel Alternative

开发者 https://www.devze.com 2022-12-20 15:52 出处:网络
In Django/Python, if I had the following model class Model1: id = char field name = char field creation_time = datetime field

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 ex开发者_运维技巧clude a field like that (without using a seperate view model)? I'm using ADO.NET Entities.


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

精彩评论

暂无评论...
验证码 换一张
取 消