Posts

Showing posts with the label Asp.Net MVC

Jquery serer side datatables in asp.net

In this post we will learn how we can implement jquery   server side datatables in asp.net mvc application. We need to add datatables library to our page, we can add references of libray from datatables <a href="https://datatables.net/" target="_blank">website</a>. We need to create an html table on which we will apply datatable. <table class="table table-striped table-bordered table-hover table-checkable order-column dataTable" id="tblInventoryItems">   <thead>     <tr>       <th>Item Name</th>       <th>Item Price</th>       <th>Item Quantity</th>       <th></th>     </tr>   </thead>   <tbody></tbody> </table> Then we need the code which will apply the datatable to above html table. Below is the code to apply datatable $("yourtableid").DataTable({         "p...

Upload file in ASP.NET MVC

Uploading files is a common task in websites. Let’s take a look on how we can upload file in ASP.NET MVC . View Code < form enctype ="multipart/form-data" method ="post">     < input type ="file" name ="fileUser" />     < input type ="submit" value ="Submit" /> </ form > Controller Code   [ HttpPost ] public ActionResult Index( HttpPostedFileBase fileUser) {   if (fileUser != null )   {     fileUser.SaveAs( Path .Combine(HttpContext.Server.MapPath( "~/UserImages" ), fileUser.FileName));   }    return View(); } Points to remember   Form should have attribute enctype ="multipart/form-data" . File element should have a name attribute. Value of  name attribute  should be  same as which we are receiving in controller. Otherwise it will be null . ...

Check if ViewBag is null or doesn't exist

ASP.NET MVC uses  ViewBag to store some data from Controller and than accessed in View like you can save UserName in ViewBag who is logged in to your website and than display it in view. Sometimes you need to check if ViewBag exists or it is not null than execute your next piece of code. Let's take an example. Controller Code public ActionResult Index() { ViewBag . UserName = "John"; return View(); } View Code @if ( ViewBag . UserName ! = null ) { < label >@ViewBag.UserName</ label >;     } Here if ViewBag.UserName is not null than label will be rendered with username. If it is null the code will not execute. So you can easily check for a ViewBag Property and execute your code.