开发者

Preparing models for ASP.NET MVC views

When returning strongly typed models for views such as Create and Edit (when validation of the object we are editing fails) I usually prepare the models like this:

    //
    // GET: /Invoice/Create
    public virtual ActionResult Create()
    {
        // prepare the empty model
        Invoice model = new Invoice();
        model.Client = new Client();
        model.Client.PostCode = new PostCode();
        return View(model);
    }

    //
    // POST: /Invoice/Create
    [HttpPost]
    public virtual ActionResult Create(Invoice document,
                                       FormCollection collection)
    {

        // check for errors
        if (!ViewData.ModelState.IsValid)
        {
            document.Client = new Client();
            document.Client.PostCode = new PostCode();
            return View(document);
        }

Now I know that this is how others do it too, in fact you can see this same approach in MVC Music Store sample and others. However, this is very error prone because one might accidentally left out a referenced entity which is required in the view. It also req开发者_运维知识库uires too much thinking about view/model interaction. What I would want is some sort of automatism. Value typed properties in models usually aren't the problem because they default either to zero or empty strings. Reference types however should be initialized with new..but sooner or later we end up with code blocks that are being repeated, reference type properties being left out, etc..And I don't think it's good coding practice either.

What are other options we could take?

UPDATE:

Because replies kinda missed the point (they do not relief us of thinking about models in any way and require additional code in model classes), I was thinking if this option would work:

  1. Use custom Action filter,
  2. override OnActionExecuted()
  3. use Reflection inside this method to take out the object from the Model and enumerate its public properties and try to initialize them.

I have steps 1, 2 and 3 partially implemented but I cannot figure out how to do "... = new Client();" programatically with Reflection.


Make the properties of your model return a new instance if it is null

private Client client;
public Client Client
{
  get
  {
    if (client == null)
      client = new Client();

    return client;
  }
}


I suggest that you use a Strongly typed view bound to a ViewModel that is distinct from the Domain Model you're trying to create, and put whatever necessary logic into the constructor of the ViewModel


I'm not sure I fully understand your question. You want what automated? ViewModels and Views? Are you creating strongly typed views?

I have created a T4 template that I point to a database and it generates a ViewModel for every table. Foreign keys become drop down lists, long strings get a TextArea instead of TextBox, etc. I then delete the ones I don't need and modify the ones I want to keep. It's not a totally automated process, but it does 80 to 90 percent of the work, depending upon the project.

Then I generate strongly typed Views from those ViewModels.

It also sounds like you might be interested in AutoMapper.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜