I have authorization form for my website:
Inside form:
<div class="auth">
#* <h1 class="auth__header">New User</h1> *#
<div class="auth__form">
<EditForm class="form" Model="#us" OnValidSubmit="#OnValidSubmit">
<DataAnnotationsValidator/>
<label class="auth__label">Name :</label>
<InputText class="input auth__input" #bind-Value="us.Name"/>
<ValidationMessage For="#(() => us.Name)" />
<label class="auth__label">Surname :</label>
<InputText class="input auth__input" #bind-Value="us.Surname"/>
<ValidationMessage For="#(() => us.Surname)" />
<label class="auth__label">Email :</label>
<InputText class="input auth__input" #bind-Value="us.Email"/>
<ValidationMessage For="#(() => us.Email)" />
<label class="auth__label">Password :</label>
<InputText class="input auth__input" #bind-Value="us.Password"/>
<ValidationMessage For="#(() => us.Password)" />
<button>#ButtonText</button>
</EditForm>
</div>
Problem is that it's shown inside site in the #Body in Shared blocks, but I need it as a another page as in this example (Blazor with individual Accounts), and I don't know how to do it:
But in this example, authorization form is a separate element, not subject to customization, and I didn't find examples or resources on the internet that had their own form.
Thanks for answers.
Create new razor page with this code:
#inherits LayoutComponentBase
<div>
#Body
</div>
And paste this into the page you need (for my example create.razor):
#layout LoginLayout
And this will be worked.
Related
This editform allows the user to edit a product from my products table. The data will then be updated to the database. This worked fine. Then I added my final 'comment' box. See I have an audit table which will record when any edits are made to the products. The audit record contains: an ID, the ID of the product being changed, the ID of the user making the change, the audit date and a comment. The validation of the other fields e.g. product.productname works but now I have a comment box where the validation is not being checked and is causing an error. I think it has to do with the fact that a Model is passed so that the models validation can be checked but when I tried to add a second model for the audit everything went red.
<EditForm Model=#selectedProduct OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<label for="productName">Name:</label>
<InputText name="productName" #bind-Value="selectedProduct .ProductName"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductName)" />
<label for="productDescription">Description:</label>
<InputText name="productDescription" #bind-Value="selectedProduct .ProductDescription"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductDescription)" />
<label for="additionalInformation">Additional Information:</label>
<InputText name="additionalInformation" #bind-Value="selectedProduct.AdditionalInformation"></InputText>
<ValidationMessage For="#(() => selectedProduct.AdditionalInformation)" />
<label for="price">Price:</label>
<InputText name="price" #bind-Value="selectedProduct.ProductPrice"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductPrice)" />
<label for="auditComments">Comments:</label>
<InputText name="auditComments" #bind-Value="auditProduct.Comments"></InputText>
<ValidationMessage For="#(() => auditProduct.Comments)" />
<button type="submit" class="btn-primary">Save</button>
<button type="submit" class="btn-secondary" #onclick="HandleCancel">Cancel</button>
</div>
</EditForm>
It appears that you are attempting to add validation to your audit table's "comments" column, however the validation is not operating. It appears that you also wish to validate the auditProduct model while using the DataAnnotationsValidator component to validate the selectedProduct model.
You must create a DataAnnotationsValidator component and give the auditProduct model to the EditForm component separately in order to add validation for it.
<EditForm Model=#selectedProduct AuditModel=#auditProduct OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<DataAnnotationsValidator Model="auditProduct" />
<!-- rest of the form fields -->
</EditForm>
I'm currently working on a project for one of my classes and I suddenly received this error when I tried to run it:
Severity Code Description Project File Line Suppression State
Error
CS0411 The type arguments for method 'IModelExpressionProvider.CreateModelExpression<TModel, TValue>(ViewDataDictionary, Expression<Func<TModel, TValue>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. Proj1BankApp C:\Users\jross.000\source\repos\Proj1BankApp\Views\Home\Index.cshtml 1 Active
Visual Studio Screenshot
It directs me to my Index.cshtml file but nothing else and I don't know how to fix it, so any help would be appreciated.
Index.cshtml:
#model BankAppModel
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 50px">
<form id="form1" runat="server">
<h1>#ViewBag.Title</h1>
<div class="row">
<label asp-for="Name" class="control-label col-sm-3">Name: </label>
<input asp-for="Name" class="form-control col-sm-3" />
<span asp-validation-for="#Model.Name" class="text-danger col"></span>
</div>
<div class="row">
<label asp-for="TransactionMonth" class="control-label col-sm-3">Month: </label>
<input asp-for="TransactionMonth" class="form-control col-sm-3" />
<span asp-validation-for="#Model.TransactionMonth" class="text-danger col"></span>
</div>
<div class="row">
<label asp-for="TransactionDay"class="control-label col-sm-3">Day: </label>
<input asp-for="TransactionDay" class="form-control col-sm-3" />
<span asp-validation-for="#Model.TransactionDay" class="text-danger col"></span>
</div>
<div class="row">
<label asp-for="TransactionYear" class="control-label col-sm-3">Year: </label>
<input asp-for="TransactionYear" class="form-control col-sm-3" />
<span asp-validation-for="#Model.TransactionYear" class="text-danger col"></span>
</div>
<div class="row">
<label class="control-label col-sm-3">Balance: </label>
<input class="form-control col-sm-3" name="balance" readonly />
</div>
<div class="row">
<input class="col offset-sm-3 pl-0" type="submit" name="deposit" value="Deposit" asp-for="Deposit" formmethod="post" />
<input class="col offset-sm-3 pl-0" type="submit" name="withdraw" value="Withdraw" asp-for="Withdraw" formmethod="post" />
<input class="btn btn-secondary" asp-action="Index" type="submit" name="clear" value="Clear" formmethod="post" />
</div>
</form>
</body>
</html>
In my case there was a hidden input property was declared, which was not declared in model class, so i removed that hidden input and error was gone,
this happens when you copy paste the code from another form. than you.
This is likely on a line like InputFor(m=> m.Something)
Actual method you're calling has generic arguments, like Method<T>(T arg1, ..) and the compiler attempts to infer them by looking at the types (the class) of the arguments you pass in..
for example: if you call a method with signature Method<T>(T value) as Method("hi") the compiler is smart enough to figure out this is actually Method<string>("hi")
But this doesn't always work, for example if the type of the argument cannt be resovled at compile time.
You likely have to find this call in your cshtml file and either add the type <typehere> manually or you may have a different compile error in your code which prevents the compiler from resolving this type.
Try scrolling down to the bottom of the error list and see if there are any other errors you can resolve. If not, look for the above.
this is my html code in which i have used two radio buttons and i want a required field validator which will force a user to select atleast one radio button .
<div class="form-group">
<label for="enterType" class="col-sm-2 control-label">Select Type
</label>
<div class="col-sm-10">
<asp:RadioButton ID="rbEncryption" runat="server"
GroupName="grpEncryption" Text="Encryption (e.g. welcome!, Enter
plain text)" />
</div>
<label for="enterType" class="col-sm-2 control-label" >
</label>
<div class="col-sm-10">
<asp:RadioButton ID="rbDecryption" runat="server"
GroupName="grpEncryption" Text="Decryption (e.g. welcome!, Enter
Encrypted text)" />
</div>
</div>
How can I display the value of an attribute in a label using tag helpers? I want to display created timestamp, but when I use the below code it just displays "Created" twice instead of "Created" and the actual timestamp.
<div class="form-group">
<label asp-for="Created" class="col-md-2 control-label"></label>
<div class="col-md-10">
<label asp-for="Created" class="form-control"></label>
</div>
</div>
Result:
Created Created
Desired Result:
Created 04/13/2017 12:00:00
You could probably just dump it, assuming you are using razor:
<div class="form-group">
<label asp-for="Created" class="col-md-2 control-label"></label>
#Created
</div>
If this needs to be placed within a form with the value posted back, you'll need an input (or use #Charlie's answer and an extra hidden input for your Created property):
<div class="form-group">
<label asp-for="Created" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Created" class="form-control my-label-class" type="datetime" />
</div>
</div>
Refer to this stackoverflow to create the css.
I'm migrating a ASP.NET 5 app to ASP.NET core. Everything is going well except that I can't submit a form.
When I do, looking at the Chrome's network timeline, the controller URL is reached (/MyController/Create) but nothing happens (The browser is idling, waiting).
After looking for some explanations: trying to call a parameterless controller works fine. Adding [FromForm] returns a 415. [FromData] idle too...
I can't find anything on the internet. Here too! (For the first time during my entire life).
NB: Everything was working fine using ASP.NET 5 MVC 6. Same code on both side.
Here is my controller code:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(SocieteGroupe societeGroupe)
{
if (ModelState.IsValid)
{
_context.SocieteGroupe.Add(societeGroupe);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewData["Id_Devise"] = new SelectList(_context.Devise, "Id", "Code_ISO", societeGroupe.Id_Devise);
return View(PackageUtils.GetViewPath(viewPath, OperationType.CREATE), societeGroupe);
}
And my view :
<form asp-controller="SocieteGroupes" asp-action="Create">
<div class="form-horizontal">
<h4>SocieteGroupe</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Id_Devise" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="Id_Devise" class ="form-control" asp-items="ViewBag.Id_Devise"></select>
</div>
</div>
<div class="form-group">
<label asp-for="Id_Salesforce" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Id_Salesforce" class="form-control" />
<span asp-validation-for="Id_Salesforce" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="Nom" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Nom" class="form-control" />
<span asp-validation-for="Nom" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
Does someone already faced out this problem or got an idea about it?
EDIT:
Instead of using a complexe type as parameter. I tried (for debug purposes) to use a string instead and return the string in Json format in order to debug it. Whatever I use (nothing, [FromForm], [FromBody]...), the result is null. I don't know what to do next. Any help will be appreciated :D
Thanks in advance :)
I found a workaround. This is a known bug of RC2.
According this link, you need to download this file and put it wherever you want in your project. Then you just need, in Startup.cs to add the line services.AddSingleton<IModelBinderFactory, MyModelBinderFactory>();
And that's all! For me, it worked :D