MarcoDeMagistris
MarcoDeMagistris
Reputation Top 1%
Marco De Magistris
10 Snippets  (94th place)
Published
1 Channel
Created
9 Channels
Following
6236 points  (19th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Topic Creator
Junior Topic Hub
Junior Trend Maker
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Junior Famous Coder
Senior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by MarcoDeMagistris

public by MarcoDeMagistris  3236  1  7  0

MVC4 autocomplete

Example: How to create a autocomplete method in MVC4
---------------------------------------------------------------------------
Index.cshtml
---------------------------------------------------------------------------
<script src="~/Scripts/jquery-ui-1.8.20.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.8.20.min.js"type="text/javascript"></script>
<link href="~/Content/jquery-ui-1.10.3.custom.min.css" rel="stylesheet"/>

<script>
    $(function () {
        $("#newCodeText").autocomplete({
            minLength: 1,
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Action("AutoCompleteCodeSuggestions", "CorrectiveFactor")',
                    type: "POST",
                    dataType: "json",
                    data: { codeValue: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
							    // Show item.Name + ": " + item.Description
								// if selected send to controller item.Name value
                                label: item.Name + ": " + item.Description, value: item.Name  
                            };
                        }))
                    }
                })
            },
            messages: {
                noResults: "", results: ""
            },
            select: function (event, ui) { /* Get selected event */
                var splitted = (ui.item.label).split(': ');
				alert("The name: "+ splitted[0] + " the description: "+ splitted[1])  
            }
        });
    })
</script>

@Html.TextBoxFor(m => m.SapMatnr, new { id = "newCodeText"})
            
---------------------------------------------------------------------------
Controller class
---------------------------------------------------------------------------

    public class CorrectiveFactorController : Controller
    {
        //Autocomplete for CodArt
        public JsonResult AutoCompleteCodeSuggestions(string codeValue)
        {
            Collection<Person> suggestedValue = null;

            try
            {
				// FindListOfCode: This method return a Collection<Person>
                suggestedValue = ClsInfoForViews.getPersonName(codeValue); 
                return Json(suggestedValue, JsonRequestBehavior.AllowGet);
            }
            catch (MethodAccessException)
            {
                return Json(suggestedValue, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult SelectDescription(string codeValue)
        {
            try
            {   /* ClsInfoForViews.SelectDescription = value*/
                string description = ClsInfoForViews.SelectDescription(codeValue);
                return Json(new { Description = description }, JsonRequestBehavior.AllowGet);
            }
            catch (MethodAccessException)
            {
                return Json(new { Description = "" }, JsonRequestBehavior.AllowGet);
            }
        }
;

public by MarcoDeMagistris  3399  2  6  0

Upload File

Upload File MVC4 patterns
Controller:
        public ActionResult Index()
        {
            return View();
        }

Model: 
		  private HttpPostedFileBase File { get; set; }
        #region PROPERTIES
        [Display(Name = "Upload your .txt File")]
        [FileExtensions(Extensions = "txt", ErrorMessage = "The file you selected is not a valid .txt file")]
        [Required(ErrorMessage = ".txt file not selected")]
        public HttpPostedFileBase File1
        {
            get { return this.File; }
            set { this.File = value; }
        }
		#endregion

Views:

	<div id="Componentsdiv">
    @using (Html.BeginForm(<action>, <Controller>, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset>
            <legend>Upload TXT File</legend>

            <div class="editor-label">
                <fieldset>
                <legend>File</legend>
                    @Html.TextBoxFor(m => m.File1, "Select new file", new {  id="firstfile", type = "file"})
                    @Html.ValidationMessageFor(m => m.File1)
                </fieldset>
            </div>
            <p>
                <input type="submit" id="submitbutton" hidden="hidden"/>
            </p>
        </fieldset>
    }
    </div>
;

public by MarcoDeMagistris  559026  64  7  0

Converts Unicode string to Ansi String

Converts Unicode string to Ansi String
public static string ConvertUnicodeToAnsi(String s)
        {
            /// <summary>
            /// Description: Converts Unicode string to Ansi String
            ///              
            /// </summary>
            /// <exception cref="ArgumentNullException"></code></exception>
            /// <exception cref="EncoderExceptionFallback"></exception>
            /// <exception cref="DecoderExceptionFallback"></exception>
            /// <exception cref="ArgumentException"></exception>
            try
            {
                byte[] unicodeBytes = Encoding.Unicode.GetBytes(s);
                byte[] ansiBytes = Encoding.Convert(Encoding.Unicode, Encoding.Default, unicodeBytes);
                return Encoding.Default.GetString(ansiBytes).ToString();
            }
            catch {
                throw;
            }
        }
;

public by MarcoDeMagistris  2469  0  6  0

Prints Jenkins Jobs

Prints Jenkins Jobs
package jenkins;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class JenkinsJob {
	
	private String m_sjenkins_url;  /*jenkins url: ex. http://localhost:8080/jenkins */
	
	public JenkinsJob(String _sjenkins_url){
		m_sjenkins_url = _sjenkins_url;
	}
	
	public void PrintJob()throws DocumentException{
		
		Document dom = new SAXReader().read(m_sjenkins_url+"/api/xml");
		
		for( Element job : (List<Element>)dom.getRootElement().elements("job")) {
        	System.out.println(job.elementText("name"));
        }
	}
}
;

public by MarcoDeMagistris  3249  19  5  0

JQuery DataPicker and TimePicker example

JQuery DataPicker and TimePicker example
<html>
<head>

<title>TBD</title>
<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script> 
<script src="http://code.jquery.com/jquery-1.10.0.js"></script>

<!-- CALENDAR Used external Library - GIT HUB https://github.com/1Marc/javascript-clean-calendar-->
<script src="js/javascript-clean-calendar-master/calendar.js"></script>
<link href="js/javascript-clean-calendar-master/calendar.css" rel="stylesheet">

<!-- TIME PICKER Used external Library - GIT HUB https://github.com/perifer/timePicker-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script src="js/timePicker-master/jquery.timePicker.js"></script>
<link href="js/timePicker-master/timePicker.css" rel="stylesheet">
<script>
jQuery(function() {
    $("#timepicker").timePicker();     // Default.
  });
</script>
<head>
<body>

<div id="DateTime_div">
		<label>Calendar </label>
		<!-- CALENDAR IMPLEMENTATION -->
		<input name="datapicker" class="calendarSelectDate" type="text">
		<div id="calendarDiv"></div>
	<p>	
		<label>Time Picker </label>				
		<!-- TIME PICKER IMPLEMENTATION -->
		<input type="text" name="timepicker" id="timepicker" size="6" />
	</p>
</div>
</body>
</head>
;