Search This Blog

Loading...

Mar 6, 2014

Delete records from Junction table


Deleting a record from Junction table is very easy, however you can not access junction table in EF directly. For example you have two tables as  Contract and Application , where Contract can have multiple application so many  to many relationship. Here is the solution to delete records from junction table in Entity framework

 // Get the records from both tables, 
//here objContract will return the records from Contract table as well as from the associated tables

  CONTRACT objContract = unitOfWork.Contract.Get(ssc => e.id == contractid).SingleOrDefault();

 // Get specific row which you want to delete from junction table
   if (objContract.JuncApplication.First().APPID == appid)
                {
                    JuncApplication rows = objContract.JuncApplication.Where(e => e.appid == appid).FirstOrDefault();
                    objContract.JuncApplication.Remove(rows);
          //Save context        
unitOfWork.Save();
                }

Feb 15, 2014

How to open Excel File in Browser using MVC

 

I am not demoing file upload approach, however you will not see any file upload control in MVC. You can open any file in MVC either directly from Disk or from database. FilePathResult help us in streaming our file to the browser. There are two ways to open Excel file in MVC

Nov 6, 2013

MultiSelect ListBox in MVC Razor

MultiSelect List box is similar to Listbox(single select) control in Razor view, with the help of multi select property you can make your single select ListBox to a MultiSelect ListBox.

Model Code:-

Declare one Property as

Code Snippet
  1. public List<int>catIds {get;set;}

I am not writing any Model code, I am using service to get the records from table. Just make sure your method should return List/IList/IEnumerable it should be enumerable. something like this

My contract should looks like this(Interface)

Code Snippet
  1. List<MyCategoryTable> MyMethodName();

and in my service method I will implement the MyMethodName, something like this

Code Snippet
  1. public List<MyCategoryTable> MyMethodName()
  2.         {
  3.  
  4.             using (var context = DataContext)
  5.             {
  6.                 var a = context.YourTableName
  7.                     //.Where(mp => mp.Colname == valuename);
  8.                     //Where clause is optional
  9.                 return a.ToList();
  10.             }
  11.         }

above method will return the list of records available in my table, we can put where clause if required, now move to the controller

Controller Code:-

Code Snippet
  1. var category = ServiceProxy.YourService.MyMethodName();
  2.             ViewBag.catList = category.ToList().
  3.                 Select(e => new SelectListItem
  4.                     { Value = Convert.ToString(e.ID),
  5.                     Text = e.Descriptions });

It’s simple, now go to your razor view and

View Code(Razor View)

Code Snippet
  1. @Html.ListBoxFor(model => model.catIds,
  2.         new MultiSelectList(ViewBag.catList,  "Value", "Text"))

That’s It.

Sep 28, 2013

Performance Optimization in Asp.net MVC 4

Performance optimization of any application is very important part and plays a major role in success of application. To get the maximum performance of your MVC application the first and very most important thing is, “you must have to define the performance matrix or Baseline of your application” and then you can compare with exact response of your application with the help of free tools such as ANTS, Google Chrome extensions, Red Gate tools etc.  Most common performance issue are occurred because of Database (when your queries are not optimized or may be too many queries are used), or due to Network resources(when you are making lots of requests from server to services or may be from browsers to web server). MVC 4.0 offers few best features to overcome this problem such as Bundling(by combining scripts and css ), Minification (by removing unnecessary wildcard and whitespace), Implementing Caching, By making proper Ajax calls etc..

Sep 1, 2013

Fill DropDown in MVC4 Razor

 

Model code

dbmodel
  1. private AccountingSystemEntities accountingSystemEntities = new AccountingSystemEntities();

Where AccountingSystemEntites is the name of my entity.

Capture

so my web.config looks like

web.config
  1. <add name="AccountingSystemEntities" connectionString="metadata=res://*/Models.cdm.csdl|res://*/Models.cdm.ssdl|res://*/Models.cdm.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=KUKU-PC;initial catalog=AccountingSystem;persist security info=True;user id=sa;password=trytry;multipleactiveresultsets=True;
  2.         App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

here I am using very simple approach so I am going to write dropdown fill method directly in the controller for practice only, it is always suggested to keep your controller clean as much as possible. All database related activities should be in Model only. My controller code looks like

CodingHubController
  1. public ActionResult CodingHubTesting()
  2.         {
  3.            
  4.             AccountingSystemEntities accountingSystemEntities1 = new AccountingSystemEntities();
  5.             ViewData["ControllerName"] = this.ToString();
  6.  
  7.             List<SelectListItem> list = new List<SelectListItem>();
  8.  
  9.  
  10.             list.Add(new SelectListItem { Text = "Please Select", Value = "0" });
  11.             var itemlists = (from c in accountingSystemEntities1.AccountDetails select c);//.ToArray();
  12.             foreach (var t in itemlists)
  13.             {
  14.                 SelectListItem s = new SelectListItem();
  15.                 s.Text = t.ItemDescription.ToString();
  16.                 s.Value = t.InvoiceDetails_Id.ToString();
  17.                 list.Add(s);
  18.             }
  19.            
  20.              ViewBag.TopicDDL = list;
  21.            
  22.             return View("CodingHubTesting", _iCodingHubRepository.GetAllAccounts());  
  23.             
  24.         }

and now in your view (right click on controller and add view) write code to create dropdown as

View
  1. @model IEnumerable<MvcApplication1.Models.AccountDetail>
  2.  
  3. @{
  4.     
  5.     ViewBag.Title = "Coding Hub- Practice pad";
  6. }
  7. <hgroup class="title">
  8.     <h1>@ViewBag.Title.</h1>
  9.     <h2>@ViewBag.Message</h2>
  10. </hgroup>
  11.       <p>@Html.ActionLink("Create", "CreatenNewItems", "Home")</p>
  12. <article>
  13.     <p class="TopicList">
  14.         @Html.Label("Topics")
  15.         @Html.DropDownList("SubCategoryId", (IEnumerable<SelectListItem>)ViewBag.TopicDDL,
  16.         new { id = "SubCategoryId" })
  17.     </p>

run your project, ready to launch!!!

Capture

Follow by Email