There are a lot of concepts to get your head around. Lets start with two basic choices. Update date using a simple post back or JavaScript and Ajax?
OK, lets go for the first one, a simple create process.
You will need the database table to update. You can create an Entity Framework model from the database. Your application will interact with this up update the table. This is the "M" or model in MVC. You'll also need to create a controller (the "C" in MVC) and a view (the "V" in MVC).
The controller will often contain two methods to perform the work. When a user navigates to the web page, they GET the initial data. All this does is return the view with a null model, so any edit controls are in their default state - e.g. empty text box
[HttpGet]
public ActionResult Create()
{
return View();
}
When the view is saved by the user, another method (with the same name) is called. Note that this is the POST method, so all of the data in the FORM is packaged up into the ModelState. MVC will bind your model for you into the parameters of the method - you can even say which ones to include. The version here uses Async.
The FORM in your view should have an anti-forgery token too, which is the first (hidden) field returned and used by the MVC system - you don't usually see it.
Assuming the data is valid, it is saved to the database (I usually have an underlying business model to do the work to keep the controller clean). If the model is not valid (e.g. missing field), the existing model is passed back to the view for the user to have another go, along with any error messages. Have a look at Data-Annotations to see how to define the valid value ranges.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "YourName")] MyData model)
{
if (ModelState.IsValid)
{
await _bs.MyData_UpdateAsync(model);
return RedirectToAction("Index");
}
// return with any errors
return View(model);
}
Finally, you'll need a view from which HTML is dynamically generated and passed to the client browser
@model MyApp.Models.MyData
@{
ViewBag.Title = "Create Something";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container w-50">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.YourName, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.YourName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.YourName, "", new { @class = "text-danger" })
</div>
<div class="d-flex">
<div class="ml-auto">
<input type="submit" value="Create" class="btn btn-primary btn-sm btn-default" />
</div>
</div>
</div>
}
</div>