Search code examples
c#.netasp.net-mvcrazor

ASP.NET MVC view manipulated model data immediately


I have an ASP.NET MVC application with a model which is bound to a view. I want to view the manipulated data immediately without Javascript or postback. Is this possible?

In "current count" below it always shows zero.

My model:

public class CounterModel
{
    public int Number { get; set; }
}

My view (Counter.cshtml):

@model CounterModel

<h4>Counter</h4>

<p role="status">Current count: @Model.Number</p>

<input type="number" class="form-control" asp-for="Number" />

My controller (HomeController.cs):

public IActionResult Counter()
{
    CounterModel model = new() { Number = 0 };

    return View(model);
}

Result:

enter image description here


Solution

  • The ASP.NET MVC framework only returns HTML and provides no interactivity between the data models and the UI, without javascript / POSTs etc.

    If you're looking for a framework that does provide this and allows you make bindings between UI elements and models, look into Blazor.

    https://dotnet.microsoft.com/en-us/learn/aspnet/blazor-tutorial/intro