The Form post null value to the pagemodel - asp.net-core

trying to submit the commenting form,but the form send null data to my razor page.
it is the html:
<div class="comment-form-container">
<h6>Leave a Comment</h6>
<form method="post">
<div class="input-prepend">
<span class="add-on"><i class="icon-user"></i></span>
<input name="Name" class="span4" id="Name" size="16" type="text" placeholder="Name">
</div>
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span>
<input name="Email" class="span4" id="Email" size="16" type="email" placeholder="Email Address">
</div>
<input type="hidden" name="ArticleId" id="ArticleId" value="#Model.articleQueryView.Id" />
<textarea class="span6" name="Message" id="Message"></textarea>
<div class="row">
<div class="span2">
<input type="submit" class="btn btn-inverse" value="Post My Comment">
</div>
</div>
</form>
</div>
and here is the CsHtml code,when in hover the "comment" i just see the null and 0 value:
public class blog_singleModel : PageModel
{
public ArticleQueryView articleQueryView { get; set; }
private readonly ICommentApplication _commentApplication;
private readonly IArticleQuery _query;
public blog_singleModel(IArticleQuery query, ICommentApplication commentApplication)
{
_query = query;
_commentApplication = commentApplication;
}
public void OnGet(int id)
{
articleQueryView = _query.GetArticle(id);
}
public RedirectToPageResult OnPost(AddNewComment comment)
{
_commentApplication.Add(comment);
return RedirectToPage("./blog_single", new { id = comment.ArticleId });
}
}
}

comment.Id is 0 is because your articleQueryView.Id is 0.Name,Email,Message is null is because you did not set the values of them in form.I think your code is correct,but you did not set the values.
sample:

Related

how to Get the last added value in TABalance coulmn in database and show it in TSBalance input automatically when the page loaded

my html code
<div class="border backgroundWhite border-info">
<div class="row">
<div class="col-6">
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TSBalance"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TSBalance" class="form-control" id="sbalance"/>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TSupply"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TSupply" class="form-control" id="supply" value="0" onkeyup="sum()" type="text"/>
</div>
<span asp-validation-for="Shift.TSupply" class="text-danger"></span>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Shift.TTotal"></label>
</div>
<div class="col-6">
<input asp-for="Shift.TTotal" class="form-control" readonly id="trbalance" value="0" onkeyup="sum()" type="text"/>
</div>
<span asp-validation-for="Shift.TTotal" class="text-danger"></span>
</div>
</div>
<div class="col-6">
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TCBalance"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TCBalance" class="form-control" id="calcebalance" value="0" onkeyup="sum()" type="text" readonly/>
</div>
</div>
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TABalance"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TABalance" class="form-control" id="actualebalance" value="0" onkeyup="sum()" type="text" />
</div>
</div>
<div class="form-group row">
<div class="col-7">
<label asp-for="Shift.TDifferance" class="text-info"></label>
</div>
<div class="col-5">
<input asp-for="Shift.TDifferance" class="form-control" style="background-color:blue;color:white;font-size:larger" id="differance" value="0" onkeyup="sum()" type="text" readonly />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
my page model
namespace Elwady.Pages.ShiftClosing
{
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
public SelectList Exlist { get; set; }
[BindProperty]
public Shift Shift { get; set; }
public CreateModel(ApplicationDbContext db)
{
_db = db;
}
public IActionResult OnGet()
{
this.Exlist = new SelectList(_db.ExpensesList, "Id", "ExName");
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
};
_db.Shift.Add(Shift);
await _db.SaveChangesAsync();
return RedirectToPage("../Index");
}
}
}
my data table
public class Shift
{
public int Id { get; set; }
public int TSBalance { get; set; }
public int TSupply { get; set; }
public int TTotal { get; set; }
public int TCBalance { get; set; }
public int TABalance { get; set; }
public int TDifferance { get; set; }
}
attached image to show clearly how you can support
i tried to get this value through assigning it to variable in get handler but i can't and tried to Asp-for model variable but i get an error in the Ui
if i need to print out this form after submit to specific printer, how can i do that?

Blazor - Client can't use model defined in dll

I'm having problem trying to use a model which is defined in a dll. I have imported the namespace to the model in _imports.razor. But still when I'm trying to use it on a page i get the error 'The name 'UserRegisterViewModel' does not exist in the current context'. Does anyone know what the problem might be? Thanks!
Edit:
I get the error in the htlm when trying to use UserRegisterViewModel, for example <EditForm Model="UserRegisterViewModel" OnValidSubmit="HandleRegistration">
Model
public class UserRegisterViewModel
{
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
Page
#page "/register"
#inject IAuthService AuthService
#inject NavigationManager NavigationManager
<h1>Register</h1>
#if (ShowErrors)
{
<div class="alert alert-danger" role="alert">
#foreach (var error in Errors)
{
<p>#error</p>
}
</div>
}
<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<EditForm Model="UserRegisterViewModel" OnValidSubmit="HandleRegistration">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="email">Email address</label>
<InputText Id="email" class="form-control" #bind-Value="UserRegisterViewModel.Email" />
<ValidationMessage For="#(() => UserRegisterViewModel.Email)" />
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText Id="password" type="password" class="form-control" #bind-Value="UserRegisterViewModel.Password" />
<ValidationMessage For="#(() => UserRegisterViewModel.Password)" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
</div>
#code {
private UserRegisterViewModel RegisterModel = new UserRegisterViewModel();
private bool ShowErrors;
private IEnumerable<string> Errors;
private async Task HandleRegistration()
{
ShowErrors = false;
var result = await AuthService.Register(UserRegisterViewModel);
if (result.Successful)
{
NavigationManager.NavigateTo("/login");
}
else
{
Errors = result.Errors;
ShowErrors = true;
}
}
}
Thanks!
Model="UserRegisterViewModel" your binding to the type not the instance RegisterModel
#page "/register"
#inject IAuthService AuthService
#inject NavigationManager NavigationManager
<h1>Register</h1>
#if (ShowErrors)
{
<div class="alert alert-danger" role="alert">
#foreach (var error in Errors)
{
<p>#error</p>
}
</div>
}
<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<EditForm Model="RegisterModel" OnValidSubmit="HandleRegistration">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="email">Email address</label>
<InputText Id="email" class="form-control" #bind-Value="RegisterModel.Email" />
<ValidationMessage For="#(() => RegisterModel.Email)" />
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText Id="password" type="password" class="form-control" #bind-Value="RegisterModel.Password" />
<ValidationMessage For="#(() => RegisterModel.Password)" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
</div>
#code {
private UserRegisterViewModel RegisterModel = new UserRegisterViewModel();
private bool ShowErrors;
private IEnumerable<string> Errors;
private async Task HandleRegistration()
{
ShowErrors = false;
var result = await AuthService.Register(RegisterModel);
if (result.Successful)
{
NavigationManager.NavigateTo("/login");
}
else
{
Errors = result.Errors;
ShowErrors = true;
}
}
}

radio button validation not working asp.net core 3.1

I have three input filed (text - tow radio button) for one property asp-validation-for works only for the input text I want to validate radio button also if it is not checked ,Model :
[BindProperty,Required(ErrorMessage = "Required")]
public string PriceAfter { get; set; }
Index.cshtml :
<div class="form-group">
<input type="number" class="form-control"
asp-for="Ad.PriceAfter">
<label class="custom-control custom-radio mb-2 ml-4">
<input type="radio" class="custom-control-input"
value="new" asp-for="Ad.PriceAfter" id="PriceAfter">
<span class="custom-control-label">new</span>
</label>
<label class="custom-control custom-radio mb-2">
<input type="radio" class="custom-control-input"
value="Used" asp-for="Ad.PriceAfter" id="PriceAfter">
<span class="custom-control-label">
Used </span>
</label>
<span asp-validation-for="Ad.PriceAfter" class="text-danger"></span>
</div>
I think you can define another field in your class. One is for the input text field and the other is for radio button.
Model:
public class MyViewModel
{
public Ad Ad { get; set; }
}
public class Ad
{
[BindProperty, Required(ErrorMessage = "Required")]
public string PriceAfter { get; set; }
[BindProperty, Required(ErrorMessage = "Required")]
public string Status { get; set; }
}
View:
<form method="post">
<div class="form-group">
<input type="number" class="form-control "
asp-for="Ad.PriceAfter">
<span asp-validation-for="Ad.PriceAfter" class="text-danger"></span>
<label class="custom-control custom-radio mb-2 ">
<input type="radio" class="custom-control-input"
value="new" asp-for="Ad.Status" id="PriceAfter">
<span class="custom-control-label">new</span>
</label>
<label class="custom-control custom-radio mb-2">
<input type="radio" class="custom-control-input"
value="Used" asp-for="Ad.Status" id="PriceAfter">
<span class="custom-control-label">
Used
</span>
</label>
<span asp-validation-for="Ad.Status" class="text-danger"></span>
</div>
<input type="submit" value="submit" />
</form>
Controller:
[HttpPost]
public ActionResult Index(MyViewModel myViewModel)
{
if (!ModelState.IsValid)
{
return View(myViewModel);
}
return Content("Ok");
}
Result:

MVC ASP.NET Core 3.1 POST to Create method does not populate model

I have used Visual Studio 2019 to create a Create view (right-click > Add > View) and set the correct model when doing so. After the Create view was created, I added code to the Create HttpPost ActionMethod and just tried to run the app. The ActionMethod is hit, but the data model that is supposed to be passed in to the method is not populated; everything is nulls despite having entered data in the browser. What is weird is that I have other pages in the app created the exact same way (even other Creates on other Areas) that work fine. What am I missing?
Model:
public class BondsAllProjectsModel
{
[Key]
public int ProjectID { get; set; }
[Display(Name = "Project Name")]
[Required (ErrorMessage = "Project Name is required.")]
[StringLength(50)]
public string ProjectName { get; set; }
[Display(Name = "Project Name Alias")]
[StringLength(50)]
public string ProjectNameAlias { get; set; }
[StringLength(50)]
public string Municipality { get; set; }
[StringLength(4)]
public string ProjectCode { get; set; }
public bool IsProject { get; set; }
[RegularExpression(#"^[0-9]*\.?[0-9]*", ErrorMessage = "Only numbers are allowed.")]
public double? ProjectAcres { get; set; }
[RegularExpression(#"\d+", ErrorMessage = "Only numbers are allowed.")]
public double? ProjectUnits { get; set; }
public int? MunicipalityID { get; set; }
[DataType(DataType.Date, ErrorMessage = "Project Completed must be a valid date.")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? ProjectCompleted { get; set; }
[MaxLength(100)]
public string CurrentNotes { get; set; }
public int? ProjectTypeID { get; set; }
public bool Discontinued { get; set; }
}
View:
#model BOnlineMVC.Models.BondsAllProjectsModel
#{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>BondsAllProjectsModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ProjectID" class="control-label"></label>
<input asp-for="ProjectID" class="form-control" />
<span asp-validation-for="ProjectID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectName" class="control-label"></label>
<input asp-for="ProjectName" class="form-control" />
<span asp-validation-for="ProjectName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectNameAlias" class="control-label"></label>
<input asp-for="ProjectNameAlias" class="form-control" />
<span asp-validation-for="ProjectNameAlias" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Municipality" class="control-label"></label>
<input asp-for="Municipality" class="form-control" />
<span asp-validation-for="Municipality" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectCode" class="control-label"></label>
<input asp-for="ProjectCode" class="form-control" />
<span asp-validation-for="ProjectCode" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsProject" /> #Html.DisplayNameFor(model => model.IsProject)
</label>
</div>
<div class="form-group">
<label asp-for="ProjectAcres" class="control-label"></label>
<input asp-for="ProjectAcres" class="form-control" />
<span asp-validation-for="ProjectAcres" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectUnits" class="control-label"></label>
<input asp-for="ProjectUnits" class="form-control" />
<span asp-validation-for="ProjectUnits" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="MunicipalityID" class="control-label"></label>
<input asp-for="MunicipalityID" class="form-control" />
<span asp-validation-for="MunicipalityID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectCompleted" class="control-label"></label>
<input asp-for="ProjectCompleted" class="form-control" />
<span asp-validation-for="ProjectCompleted" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CurrentNotes" class="control-label"></label>
<input asp-for="CurrentNotes" class="form-control" />
<span asp-validation-for="CurrentNotes" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectTypeID" class="control-label"></label>
<input asp-for="ProjectTypeID" class="form-control" />
<span asp-validation-for="ProjectTypeID" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="Discontinued" /> #Html.DisplayNameFor(model => model.Discontinued)
</label>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
Controller:
[HttpPost]
public ActionResult Create(BondsAllProjectsModel projectData)
{
try
{
if (ModelState.IsValid)
{
int newProjectID = bDAL.AddProject(projectData);
return Redirect("~/Bonds/ProjectMaintenance/Create2/" + newProjectID);
}
else
{
return View();
}
}
catch
{
return View();
}
}
You can try to explicitly specify where model binder should looks when binding data by using
[FromForm] attribute
click here for more info
top of the view add #model namespace.BondsAllProjectsModel
<div class="form-group">
<label asp-for="ProjectID" class="control-label"></label>
<input asp-for="#Model.ProjectID" class="form-control" />
<span asp-validation-for="ProjectID" class="text-danger"></span>
</div>
Just add this line in the top of your create view:
#model YourNamespace.BondsAllProjectsModel
There is no need to modify anything else.

How can I put red border around input without JS?

<span asp-validation-for="Password" class="text-danger"></span>
<div class="form-group">
<label asp-for="Password">პაროლი</label>
#Html.TextBoxFor(x => x.Password, new { type = "password",#class = "form-control" })
<!--
<input asp-for="Password" class="form-control" />
-->
</div>
This is Sample input. I am doing Server-side validation only. How can I make input field border red when user input is not validated?
Model Validation feature adds the CSS Class input-validation-error to all the controls that have failed the validation checks .
Here is a example you could refer to :
1.Model
public class User
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[StringLength(8, ErrorMessage = "{0} length must be between {2} and {1}.", MinimumLength = 6)]
public string Password { get; set; }
}
2.View
<style type="text/css">
.input-validation-error {
border-color: yellowgreen;
}
</style>
<div class="row">
<div class="col-md-4">
<form asp-action="Create" class="myForm">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password">პაროლი</label>
<input asp-for="Password" type="password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#section Scripts
{
#{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
Result :
Reference : https://www.yogihosting.com/aspnet-core-model-validation/