Skip to main content

Posts

Showing posts from August, 2013

How to get collections on viewmodel on postback in MVC

In mvc most of the people will face problem how to bind view model list while post request. If a partial control is binding with a list and want to preserve the user input with view model list, most common issue is id/name is same. If the Id/Name are different only MVC engine can bind it with the model. Please see the below example Build the view/partial view in the following way. By keeping a row counter means all items will have unique names. Suppose this partial view is coming inside a View Called MethodName.ASPX and is included inside the Main Page using html.beginform() [httpGet] { <%html.dropdownlist(model=>model.Test, new {onclick=Test(<%=model.Id%>);}); <.div id="dvPopulatePartialViewWithList"></.div> } Javascript function Test function Test(id) {  $.ajax({         type: "Get",         data: "Id=" + Id,         url: '/MyController/GetPartialViewListById',         succes

Entity framework practices

People who are using entity framework for Dal single dbcontext is going to be a problem in BLL. I use the following structure   I used to get the single dbContext by:- Each single repo class should inherit from base repo class and in base repo class should initialise the dbcontext.   The BaseRepository     public class BaseRepo     {        private readonly bool isNewInstance;        protected OnlineProductLinkEntitiesdbContext;            protected BaseRepository()        {            dbContext = newOnlineProductLinkEntities();            isNewInstance = true;        }          protectedBaseRepository(OnlineProductLinkEntitiesdbContext)        {            isNewInstance = dbContext == null;              if (isNewInstance)                dbContext = newOnlineProductLinkEntities();              this.dbContext = dbContext;        }          protected voidCheckAndTryToOpenConnection()        {            if (dbContext == null)                dbContext = newOnlineProductLinkEntities();