Feeds:
Posts
Comments

Archive for the ‘MVC’ Category

create thumbnail images in mvc3

public void CreatThumbnailCat(HttpPostedFileBase fileName)
{

string Thumboutput = Server.MapPath(“~/UploadImages/Category/Thumbnail/” + fileName.FileName.ToString());
string OrgImage = Server.MapPath(“~/UploadImages/Category/” + fileName.FileName.ToString());

// Load image.
Image image = Image.FromStream(fileName.InputStream, true, true);

// Save Orginal image
image.Save(OrgImage);

// Compute thumbnail size.
Size thumbnailSize = GetThumbnailSizeCat(image);

// Get thumbnail.
Image thumbnail = image.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);

// Save thumbnail.
thumbnail.Save(Thumboutput);

}

static Size GetThumbnailSizeCat(Image original)
{
// Maximum size of any dimension.
const int maxPixels = 54;

// Width and height.
int originalWidth = original.Width;
int originalHeight = original.Height;

// Compute best factor to scale entire image based on larger dimension.
double factor;
if (originalWidth > originalHeight)
{
factor = (double)maxPixels / originalWidth;
}
else
{
factor = (double)maxPixels / originalHeight;
}

// Return thumbnail size.
return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
}

[HttpPost]
public ActionResult AddCatagory(AddCatagory AddModel, FormCollection FormClct)
{

if (ModelState.IsValid)
{
try
{
# region Imagesection
string ImageName = string.Empty;

foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
CreatThumbnailCat(file);
ImageName += file.FileName.ToString();
}
}
# endregion

}
}

Read Full Post »

<script type=”text/javascript”>
$(document).ready(function () {
$(‘.gc_table tr a’).click(function (e) {
//            e.preventDefault();

var divId = $(this).attr(‘id’);
if (confirm(‘Are you sure you want to remove this related item??’)) {
if ($(“#tr” + divId)) {
$(“#tr” + divId).hide();
}
}

});
});

</script>

//html

 

<table cellspacing=”0″ cellpadding=”0″ style=”width: 600px;”>
<thead>
<tr>
<th>
Product Name
</th>
<th>
</th>
</tr>
</thead>
<tbody id=”product_items_container”>
<tr id=”trrelated_product_Microwave”><td><input type=”hidden” value=”Microwave Oven” name=”related_products[]”>Microwave Oven</td><td><div><a onclick=”remove_related_product(Microwave Oven); return false;” id=”related_product_Microwave” href=”/Catalog/EditProduct/82#&amp;tab-related”>Remove</a></div></td></tr><tr id=”trrelated_product_Convection”><td><input type=”hidden” value=”Convection Oven” name=”related_products[]”>Convection Oven</td><td><div><a onclick=”remove_related_product(Convection Oven); return false;” id=”related_product_Convection” href=”/Catalog/EditProduct/82#&amp;tab-related”>Remove</a></div></td></tr></tbody>
</table>

Read Full Post »

Save images to database or folder in asp.net mvc3

View

@using (Html.BeginForm(” EditProduct”, “Catalog”, FormMethod.Post, new { @class = “block-content form”, @id = “complex_form”, enctype = “multipart/form-data” }))
{

choose File:

Save

}

Controller

[HttpPost]
public ActionResult EditProduct(FormCollection collection)
{
HttpPostedFileBase file = Request.Files[0];
byte[] imageSize = new byte[file.ContentLength];
file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
string image = file.FileName.Split('\\').Last();
int size = file.ContentLength;

if (size > 0)
{
file.SaveAs(Server.MapPath("~/UploadImages/" + image.ToString()));
//Save image url to database
}

return View();
}

Read Full Post »

Dropdown list in asp.net Mvc 3  using Razor

1)Create Model
public class AddCatagory
{
public List<SelectListItem> ParentList { get; set; }
[DisplayName("Parent")]
public int parent { get; set; }
}

2) Controller
public ActionResult AddCatagory(AddCatagory AddModel)
{
AddModel.ParentList = parentListFu("VS");
//AddModel.parent =0 ;
return View(AddModel);// dont pass to forgot model in view
}

// hardcoded function in controller

private List<SelectListItem> parentListFu(string defaultValue)
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Nagaur", Value = "Na", Selected = (defaultValue == "na") });
items.Add(new SelectListItem { Text = "Thalanju", Value = "Th", Selected = (defaultValue == "th") });

return items;
}

3)View

@model Constellation.Models.BusinessLogic.AddCatagory // include model name view

for Dropdownlist

@Html.DropDownListFor(x => x.parent, Model.ParentList, new { @class = "full-width" })

Read Full Post »

Call this function.

[HttpPost]
public ActionResult Digitalproduct(AddProduct model)
{
DiGIFUNTION();
return View();
}

      public void DiGIFUNTION()
{
string filePath = null;
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase Infile = Request.Files[inputTagName];
if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == “.xls” || Path.GetExtension(Infile.FileName) == “.xlsx” || Path.GetExtension(Infile.FileName) == “.xlsm”))
{
filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(Infile.FileName));
try
{
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
Infile.SaveAs(filePath);
}
catch (Exception ee)
{
}

//Infile.SaveAs(filePath);
}
else
{
ModelState.AddModelError(“Error”, “Only Excel Format Allowed..”);

}

if (filePath != null)
{
System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=” + filePath.ToString() + “;Extended Properties=\”Excel 12.0 Xml;HDR=YES\”;”);
oconn.Open();

try
{
if (oconn.State == System.Data.ConnectionState.Closed)
oconn.Open();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}

dynamic myTableName = oconn.GetSchema(“Tables”).Rows[0][“TABLE_NAME”];
OleDbCommand ocmd = new OleDbCommand(“select * from [” + myTableName + “]”, oconn);
OleDbDataReader odr = ocmd.ExecuteReader();
if (odr.HasRows)
{
while (odr.Read())
{

AddProduct Varproduct=new AddProduct ();
var RemoveDupProduct = CheckProductName(odr[1].ToString().Trim());
//   Varproduct.ProductTitle = odr[1].ToString().Trim();
Varproduct.Image = odr[2].ToString().Trim();
Varproduct.Description = odr[3].ToString().Trim();
Varproduct.featured = Convert.ToInt32(odr[4]);
Varproduct.Expert= odr[5].ToString().Trim();
try
{
if (RemoveDupProduct == true)
{
ModelState.AddModelError(“Error”, “Your product will be Added Soon..”);

}
else
{
ModelState.AddModelError(“Error”, “Product Already Exists”);
// Response.Write(” <br/>Product Name ” + odr[1].ToString().Trim() + ” Already exists <br/>”);

}

}
catch (Exception ee)
{
Response.Write(“Error While uploading==:” + ee.ToString());
}
}
}

}
}
}

Read Full Post »