Search code examples
razorwebmatrixwebmatrix-2razor-declarative-helpers

Webmatrix/Razor SQL query - Websecurity variable


I am trying to write a query to show all records owned by the current logged on user but i am having issues inserting the "userid" variable into the string?

@{
Layout = "~/_template1.cshtml";
var db = Database.Open("StayInFlorida");


var userid = WebSecurity.CurrentUserId;
var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID='userid'";
}

<h1>Properties - Page coming soon</h1>

@userid

@foreach (var row in db.Query(premierproperty)){
@row.propertyname
} 

Any ideas?


Solution

  • Try like this:

    @{
        Layout = "~/_template1.cshtml";
        var db = Database.Open("StayInFlorida");
        var userid = WebSecurity.CurrentUserId;
        var premierproperty = "SELECT PropertyName, PropertyID FROM PropertyInfo WHERE OwnerID = @0";
    }
    
    <h1>Properties - Page coming soon</h1>
    
    @userid
    
    @foreach (var row in db.Query(premierproperty, userid))
    {
        @row.propertyname
    }