Subscribe to andrewsw's Blog        RSS Feed

ASP.NET Core MVC Sniplets

Icon 1 Comments
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.

1 Comments On This Entry

Page 1 of 1

andrewsw Icon

10 August 2017 - 12:16 AM
This isn't a sniplet, but a curious bug and resolution. Using CORE with Entity Framework, I was attempting pagination with an expression like:

var data = _context.MyData.Skip(page * perPage).Take(perPage);

It fails with "SqlException: Incorrect syntax near 'OFFSET'".

Investigating I found suggestions that OFFSET/FETCH were available with SQL Server 2008, but not since. A suggestion was to amend an EMDX file and change "2012" to "2008" but, as I'd created the model(s) myself, this file wasn't available.

I continued to search and eventually found mention of what proved to be the solution in a Chinese blog site. Add a configuration UseRowNumberForPaging to Startup.cs:

    o => o.UseRowNumberForPaging()));

The MS prompt states "Use a ROW_NUMBER() in queries instead of OFFSET/FETCH. This method is backwards compatible to SQL Server 2005".

This is one of those cases where it was tricky to find the solution and, should it happen again in 2 years time, I'm unlikely to remember where I wrote the solution down!
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2017

151617 18 192021


    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

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