Ways to pass information from a controller to a view in ASP.NET MVC

There are three ways to pass information from a controller to a view:

  1. As a dynamic type (using @model dynamic)
  2. As a strongly typed model object.
  3. Using the ViewBag

See the sample list and three different actions for my HomeController:

List<People> peopleList = new List<People>
new People { Name = "Juan",Surname="Lazario",PhoneNumber=0539647},
new People { Name = "Cris",Surname="Tormes",PhoneNumber=0539647},
new People { Name = "Bego",Surname="Calvo",PhoneNumber=0539647},
new People { Name = "Manu",Surname="Martinez",PhoneNumber=0539647}

public ActionResult Dynamic()
return View(peopleList);

public ActionResult StronglyTyped()
return View(peopleList);

public ActionResult ViewBagAction()
ViewBag.People = peopleList;
return View();

1.As a dynamic type (using @model dynamic)

Continue reading

#asp-net, #mvc, #viewbag

Convert an oracle BLOB image (Byte[]) to base64 for web service

while (reader.Read()) {
               PhotoInfos myPhoto = new PhotoInfos();
               byte[] image = (byte[])reader[“image”];
               int imageLength = image.Length;
               string base64image = Convert.ToBase64String(image, 0, imageLength);
               myPhoto.photo = base64image;

Remove the last character of a StringBuilder object

Updating data depending on the whether or not the parameters are set may cause various problems. For example,

StringBuilder sqlCommand = new StringBuilder();
                sqlCommand(@"update myTable set ");

                    sqlKomut.Append(" id=:ID, ");
                    command.Parameters.AddWithValue(":ID", parameters.ID);
                    sqlKomut.Append(" name= :NAME, ");
                    command.Parameters.AddWithValue(":NAME", parameters.NAME);

In this case, if you send only ID to set, as it will be the last parameter in the query, it may cause an  error. By removing last comma, you can add as many parameters as you want.

                //Removing commas
                sqlCommand = new StringBuilder(sqlKomut.ToString().Trim().TrimEnd(‘,’));