I have a simple MVC model called Invitation
, it looks like this:
public class Invitation
{
public string InvitedUserDisplayName { get; set; }
public string InvitedUserEmailAddress { get; set; }
}
I want to create an user interface in the View
, using knockout.js. Where the user can input value for the 2 fields, as well as press a button that sends the invite (calling an invitation method inside my Controller, passing the Invitation
Object the user just specified.
I cannot find any solid documentation for beginners that explains this basic process. What I have so far is kind of a mess:
@Model = ViewModel.Invitation
<script src="~/lib/knockout/dist/knockout.js"></script>
@model Invitation
<form asp-controller="Home" asp-action="SendInvite" method="post">
Email: <input asp-for="InvitedUserEmailAddress" data-bind="value: invitedUserDisplayName"/> <br />
Display Name: <input asp-for="InvitedUserDisplayName" data-bind="value: invitedUserEmailAddress"/> <br />
<button data-bind="click: sendInvitation">Send Invite</button>
</form>
<script type="text/javascript">
var viewModel = {
invitedUserDisplayName: @Model.InvitedUserDisplayName,
invitedUserEmailAddress @Model.InvitedUserEmailAddress
this.sendInvitation = function () {
//call controller action, passing the newly created Invitation.
}
};
ko.applyBindings(viewModel);
</script>