Showing posts with label ASP.NET MVC 2. Show all posts
Showing posts with label ASP.NET MVC 2. Show all posts

Sunday, October 30, 2016

How to Get Complete URL using Disk File Path

Consider photosLocation as a path relative to the application; for example: "~/Images/". This way, you could use MapPath to get the physical location, and ResolveUrl to get the URL (with a bit of help from System.IO.Path):
string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation);
if (Directory.Exists(photosLocationPath))
{
    string[] files = Directory.GetFiles(photosLocationPath, "*.jpg");
    if (files.Length > 0)
    {
        string filenameRelative = photosLocation +  Path.GetFilename(files[0])   
        return Page.ResolveUrl(filenameRelative);
    }
}

Tuesday, August 2, 2016

Using Extension Methods in C#


Following is an example use of an Extension method in C#.

Imagine you have a class written by someone else and you are not allowed to do any modification to the class.

   public class Class1
    {
        public int MyProperty { get; set; }
    }

You have come up with the following calculation and want to have it in the Class1, so that others could use it without rewriting the calculation.

int sum = p.MyProperty * 5;

Now, incomes the extension method.

public static class myextention
    {
        public static int test(this Class1 p)
        {
           
            return p.MyProperty * 5;
        }
    }

After creating the extension method, you can now call the test function as it’s defined in the Class1 itself.

Class1 c = new Class1();

int extensionSum = c.test();

Wednesday, May 8, 2013

Date Format in ASP.NET MVC Form Edit Mode

You might get the date displayed in the TextBox in the below format.

09/05/2013 12:00:00 AM

To remove the 12:00:00 AM part from the text you need to do the following things.

Set the display format in date property.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime ArrivedDate { get; set; }


Modify the view as below.

<%: Html.EditorFor(model => model.ArrivedDate)%>

Monday, May 6, 2013

ASP.NET MVC Calender

Recently i was working on a MVC 2 Project and needed an date picker. I used the Jquery atepicker widget which gave me great control over picking up dates on the TextBox.

<link rel="stylesheet" href="<%= Url.Content("http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css") %>" />
  <script src="<%= Url.Content("http://code.jquery.com/jquery-1.9.1.js") %>"></script>
  <script src="<%= Url.Content("http://code.jquery.com/ui/1.10.3/jquery-ui.js") %>"></script>


  <script>
      $(function () {
          $("#DeliveryDate").datepicker({ dateFormat: "dd/MM/yy" }).val()

      });
    </script>

 <% using (Html.BeginForm()) {%>

        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

  <div class="editor-label">

               <%: Html.LabelFor(model => model.DeliveryDate) %>

            </div>

            <div class="editor-field">

                <%: Html.TextBoxFor(model => model.DeliveryDate) %>

                <%: Html.ValidationMessageFor(model => model.DeliveryDate) %>

            </div>

<% } %>

Thursday, May 2, 2013

Link CSS and Script Files in ASP.NET MVC 2

To resolve the correct file path in MVC 2 you need to use URL.Content.

Linking a CSS file.

<link href="<%= Url.Content("~/Content/StyleSheet1.css") %>" rel="stylesheet" type="text/css"/>

Linking a JavaScript file.

<script src="<%= Url.Content("~/Content/TreeMenu.js") %>" type="text/javascript"></script>

Adding Images through CSS.

.myClass  { background-image:url(<%: Url.Content("~/Content/icons/page.png")%>); }

Friday, April 26, 2013

The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])'

In ASP.NET MVC 2 you may get this error due to null value referencing model. In my case, I had the following code in my view.

<% foreach (var item in Model) { %>
<tr>
<td>
<%: item.Fax %>
</td>
</tr>
<% } %>

Using Html.Encode I managed to solve the issue.

<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.Encode(item.Fax) %>
</td>
</tr>
<% } %>

Tuesday, April 23, 2013

ASP.NET MVC Adds NULL Record

Let's say you have a controller method like below which used to submit a record to the database.
public ActionResult Create(person ob)
 { 
 if (ModelState.IsValid) 
 {
 context.Save();// save record to database
 return View(ob); 
 }
 else 
 {
 return View(ob); 
 }
 }

Above method will add a record to the database each time you load the view, that is before you submit the form. This happens because this method is used for both GET and POST requests. To avoid adding a null record modify the code as below.

[HttpPost]
public ActionResult Create(person ob)
 { 
 if (ModelState.IsValid) 
 {
 context.Save();// save record to database
 return View(ob); 
 } else 
 {
 return View(ob); 
 }
 }

[HttpGet] 
public ActionResult Create() 
{ 
 return View(); 
}

Now on the page load you will access the page via GET request and when the form is submitted POST method will fire.