Converting a List of strings to a SelectList

I had a brief list of comma-separated strings that I was storing in the web.config file, and I needed to convert them into a SelectList to bind to a DropDownListFor control in MVC. For that, there was a very simple method:

TempData["FundingTypes"] = new SelectList(ConfigurationManager.AppSettings["CoasterTypes"]
     .Split(',').ToList());

If I had needed a List<SelectListItem> instead, I would have done this:

            TempData["FundingTypes"] = ConfigurationManager.AppSettings["CoasterTypes"]
                .Split(',')
                .Select(f => new SelectListItem() {
                    Text = f.ToString(),
                    Value = f.ToString()
                }).ToList();

Leave a comment

Showing MVC ModelState errors in the view

I wanted to show the various ModelState errors encountered in an MVC controller and display them in the view. So in my controller, I have a bit of code that loops through the errors and builds them in a string, adding an HTML line break between each error:

string errors = String.Empty;

foreach (ModelState modelState in ViewData.ModelState.Values) {
     foreach (ModelError error in modelState.Errors) {
          errors += “<br />” + error.ErrorMessage;
     }
}

ViewBag.Message = “The following errors have occurred: “ + errors;

In the view, I show my ViewBag variable. Notice that I use the Html.Raw method to display the HTML code properly in using Razor:

@Html.Raw(ViewBag.Message)

 

Leave a comment

HTML select value missing from MVC model after post

In my MVC project, I have a property that is either Y or N. No maybe, no empty string – “Y” or “N”.

public class Coaster {
     public string DoYouLikeCoasters { get; set; }
}

I first thought just to do a basic select element. Something simple:

<select id=”DoYouLikeCoasters”>
     <option value=”Y”>Y</option>
     <option value=”N”>N</option>
</select>

But when I checked the model in the controller after the form was posted, the “DoYouLikeCoasters” property was empty. I basically hadn’t told MVC that I wanted the property. In order to properly get the value to the controller, I needed to MVC-ify the select by using the Html.DropDownListFor. Since I just wanted “Y” and “N” as values, I still wanted to essentially hard-code the values. So here is how I did that:

@Html.DropDownListFor(model => model.DoYouLikeCoasters, new SelectListItem[] {
     new SelectListItem() { Value = “Y”, Text = “Y” },
     new SelectListItem() { Value = “N”, Text = “N” }
}, new { @class = “form-control”, @style = “width: 50px” })

Leave a comment

Give focus to a control in a Bootstrap modal popup window

The times that try men’s’ souls is when you have a Bootstrap modal popup window, and you can’t get the control you want to get focus. So if I have a modal popup window like so:

<div id=”resultsModal” class=”modal fade” role=”dialog”>
   <div class=”modal-dialog”>
      <div class=”modal-content”>
         <div class=”modal-header”>
            <button id=”modalClose” type=”button” class=”close” data-dismiss=”modal”>&times;</button>
            <h4 class=”modal-title text-center”>Update Record</h4>
         </div>

         <div class=”modal-body”>
            <div class=”form-inline” style=”margin-left: auto; margin-right: auto; width: 400px”>
               <div class=”form-group”>
                  <label for=”theDate” >Date: </label>
                  <input type=”text” id=”theDate” class=”form-control” /><br>
                  <input type=”button” id=”save” value=”Save” onclick=”saveRecord() class=”form-control saveButton” />
               </div>
            </div>
         </div>

         <div class=”modal-footer”>
            <button type=”button” class=”btn btn-default” data-dismiss=”modal”>Cancel</button>
         </div>
      </div>
   </div>
</div>

To give focus to the date textbox, I use this little snippet:

$(“#myModal”).on(“shown.bs.modal”, function () {
     $(“#theDate”).focus();
})

 

Leave a comment

Expand a DIV width beyond its parent

I had an issue where a content management system restricted the layout of the page to have three columns. I wanted the results of a search, which were displayed in a div tag, to expand to fill (almost) the entire width of the screen. After just a bit of tweaking with padding and width, I ended with this:

<div id=”results” style=”margin-left: calc(-50vw + 50%);padding-left: 15px; width: 98vw; “></div>

Note that your mileage may vary. This particular code worked with the arrangements of elements in my page, and I have not tested it thoroughly enough to say it would work in all instances.

Leave a comment

BlogEngine.NET project locks up

I like BlogEngine.NET for a blogging platform. It’s easy to set up and use, and it appeals to the programmer side of me (which is a very large chunk) because of its extensibility.

But I was having a problem. After I extracted the files from the ZIP file and used the solution once, I could never use the project again – the three projects inside of the BlogEngine.NET solution would say “Initializing…” and would never go beyond that, and Visual Studio would completely freeze.

I found that the Visual Studio Solution User Options file (the last one in the image below) was the problem. I deleted this file, and I was able to open the solution just fine.

BlogEngine.NET files

BlogEngine.NET files

Leave a comment

Use jQuery to remove HTML table rows beyond the first

I had a table that I need to remove all but the first (header) row from. To do that, I just had a bit of jQuery to query all rows greater than (the “gt” operator) the first:

$(“#resultsTable”).find(“tr:gt(0)”).remove();

 

Leave a comment