Subscribe to andrewsw's Blog        RSS Feed

ASP.NET Core MVC Sniplets

Icon Leave Comment
We are using ASP.NET Core MVC. It is cool but finding up-to-date information is a little tricky because it is so new. Well, not brand new, but most information available is still for earlier versions.

I tend to search for "ASP.NET MVC ..." and initially assume that it hasn't changed. (Most of it hasn't changed.)

I'm not certain that some of the info that follows is specific for .NET Core, but they are just examples of where I struggled to find details but eventually found "a solution".

We wanted to populate a dropdown (a select) where the details for the values and displayed text are the same. I jumped through some hoops to duplicate the item-values:

public List<string> Streets = new List<string>();

public IEnumerable<SelectListItem> DisplayStreets
        var streetsList = Streets.Select(x => new SelectListItem
            Value = x,

            Text = x


        return streetsList;

@Html.DropDownListFor(m => m.ServiceRequestLocation, Model.DisplayStreets, "Select a street", new { @class = "form-control", id = "streetList" })

While looking around I noticed there was SelectList available and also noted someone saying that the Text and Value items for this could be omitted. I made a lucky guess that I could use my List<string> to construct the SelectList:

@Html.DropDownListFor(m => m.ServiceRequestLocation, new SelectList(Model.Streets), "Select a street", new { @class = "form-control", id = "streetList" })

This reflects the HTML element behaviour that select-options will use the displayed text as the value for an option, if no value is specified.

Knowing that a List<string> can be used directly is useful. (It also resolved/dismissed an ongoing discussion about where the SelectListItems should be assembled.)

I was looking for a way to distinguish between two submit buttons. Someone suggested giving them the same name but different values, and then code like this: if (Request.Form["submit"] != null). This gave an error.

Another version was Request.Form.AllKeys.. but AllKeys wasn't available. I noticed 'Keys' was available and went with the following, which works:

if (Request.Form.Keys.Contains("search"))

When researching, have Visual Studio open (if nearby) and make good use of IntelliSense to confirm that what you are discovering is available. However, we need to go a step further with .NET Core because libraries aren't installed by default. We need to use Ctrl-. (Quick Steps) to see if a library is available.

The following sniplets for reading and writing a cookie, and converting its type, aren't new in .NET Core, it's just useful information to show here.

private readonly CookieOptions _cookieOptions = new CookieOptions { Path = "/", HttpOnly = false, Secure = false };

Guid guid;

if (!Guid.TryParse(Request.Cookies["SRGUID"], out guid))
    throw new Exception("Could not retrieve guid");

Response.Cookies.Append("SRGUID", vm.ServiceRequestId.ToString(), _cookieOptions);

I'll try and come back and update this entry if I come across other useful small fragments.

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

July 2017

23 242526272829


    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)