I have the following form in the cshtml file:
<form action="@Url.Action(controllerAction, controller, new { area = area })" data-ajax="true" data-ajax-begin="OnBeginForm" data-ajax-method="POST" data-ajax-success="OnSuccess" data-ajax-failure="OnFailure" method="post">
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="alert alert-error" id="errorContainer" style="display:none;">
<span id="errorList">
</span>
</div>
<div class="form-horizontal">
<input type="hidden" asp-for="Id" value="@Model.Id" />
<div class="box-body">
<h4>
Delete City '@Model.Code'?
</h4>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" onclick="controlSubmit(this,this.form);" class="btn btn-default pull-right"><i class="fa fa-times"></i> Delete</button>
<a data-dismiss="modal" style="margin-right:15px; cursor:pointer; vertical-align:-webkit-baseline-middle">Cancel</a>
</div>
}
</form>
And my controller:
[HttpPost]
public async Task<IActionResult> Delete(CitiesDataModel model)
{
log.Info("Deleting City...");
log.Info(model);
}
My model:
public class CitiesDataModel
{
public int Id { get; set; }
public string IdEnc { get; set; }
[Display(Name = "Code")]
public string Code { get; set; }
}
The form is submitted successfully and i get the logs in the controller but the model is empty. Why is this happen?
Did you add _ViewImports.cshtml?
Please check asp-for is using the tagHelper. Otherwise, it'll not map the model. Add the line in your _ViewImports.cshtml file
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers;