How to map View Model back to Domain Model in a POST action?

I use an IBuilder interface and implement it using the ValueInjecter public interface IBuilder<TEntity, TViewModel> { TEntity BuildEntity(TViewModel viewModel); TViewModel BuildViewModel(TEntity entity); TViewModel RebuildViewModel(TViewModel viewModel); } … (implementation) RebuildViewModel just calls BuildViewModel(BuilEntity(viewModel)) [HttpPost] public ActionResult Update(ViewModel model) { if(!ModelState.IsValid) { return View(builder.RebuildViewModel(model); } service.SaveOrUpdate(builder.BuildEntity(model)); return RedirectToAction(“Index”); } btw I don’t write ViewModel I write Input … Read more

Where does the “business logic layer” fit in to an MVC application?

The way I have done it – and I’m not saying it is right or wrong, is to have my View and then a model that applies to my view. This model only has what is relevant to my view – including data annotations and validation rules. The controller only houses logic for building the … Read more

There is no ViewData item of type ‘IEnumerable’ that has the key ‘xxx’

Ok, so the answer was derived from some other posts about this problem and it is: If your ViewData contains a SelectList with the same name as your DropDownList i.e. “submarket_0”, the Html helper will automatically populate your DropDownList with that data if you don’t specify the 2nd parameter which in this case is the … Read more

Does Razor syntax provide a compelling advantage in UI markup?

[Disclaimer: I’m one of the Microsoft developers on MVC and Razor, so I might be a bit biased :)] We designed Razor to be a concise templating language that uses only the minimal necessary amount of control characters. I would say that large parts of your views can be expressed with fewer characters than the … Read more

Can I add a class to an HTML.ActionLink in MVC3

Yes, you can just add another parameter with object representing css class: Html.ActionLink(“Create New”, “Create”, CONTROLLERNAME, null, new { @class= “yourCSSclass”} ) It can be translated to: Html.ActionLink(link text, action name, controller name, route values object, html attributes object) Edit: To add custom styles, use this: Html.ActionLink( “Create New”, “Create”, CONTROLLERNAME, null, new { @class= … Read more

Purpose of ActionName

It allows you to start your action with a number or include any character that .net does not allow in an identifier. – The most common reason is it allows you have two Actions with the same signature (see the GET/POST Delete actions of any scaffolded controller) For example: you could allow dashes within your … Read more

DropDownListFor Not Selecting Value

After researching for an hour, I found the problem that is causing the selected to not get set to DropDownListFor. The reason is you are using ViewBag’s name the same as the model’s property. Example public class employee_insignia { public int id{get;set;} public string name{get;set;} public int insignia{get;set;}//This property will store insignia id } // … Read more

Fat model / thin controller vs. Service layer [closed]

All of this depends on the intention and requirements of your application. That said, here’s my suggestion for “mid scale” (not a local restaurant, and not Twitter/Facebook) web applications. Lean Domain Modeling Dry POCO style objects, preferably ignorant to the MVC architecture of your web application to remain as loosely coupled from your particular implementation … Read more

Mapping Lists using Automapper

Mapper.CreateMap<Person, PersonViewModel>(); peopleVM = Mapper.Map<List<Person>, List<PersonViewModel>>(people); Mapper.AssertConfigurationIsValid(); From Getting Started: How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type’s design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)