I'm new to ASP .NET, MVC and AngularJS so I'm trying to figure this all out still.
I have a Model called Words.cs which contains:
namespace liveSearch.Models
{
public class Words
{
public int ID { get; set; }
public string Word{ get; set; }
}
}
The database is already populated with data and I want to use JavaScript/JQuery to get every Word from the database so I can then manipulate that data in JavaScript etc.
I currently have Razor in my index.cshtml which does:
@{
foreach (var item in Model)
{
<li>
@Html.DisplayFor(modelItem => item.Word)
</li>
}
}
This works in getting and displaying all Words in the model but I want to get rid of the Razor and use JavaScript/Jquery to get the data instead.
Ideally, I want to get all Words from the DB (I don't care about the IDs) and add it to an array in the scope in the .js file.
The problem is that I can't figure out how to access the DB with JavaScript or AJAX
I think I can use $.get()
or $.ajax()
but I don't really know how to use it with a DB/model.
I also read a few other posts here on Stackoverflow that said this can be done, but I wasn't able to apply any of those examples to my case for some reason.
var words = @Html.Raw(Json.Encode(Model));
(thenwords[0].Word
will return the value of the firstWord
property in your collection. – Stephen Muecke 16 hours ago