I wanted to Customize the Manager Area, specific the User. I added a new Column(PhoneNumber) in the Database and the Data of this field gets in the UserEditModel. I added an new text field in the Edit.cshtml.
<div class="col-sm-6">
<div class="form-group">
<label>#Localizer.General["Email address"]</label>
<input type="email" v-model="userModel.user.email" class="form-control small" maxlength="128" required />
<div class="invalid-feedback">
#Localizer.General["Email address is mandatory."]
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>#Localizer.General["PhoneNumber"]</label>
<input v-model="userModel.user.PhoneNumber" class="form-control maxlength="128" required />
<div class="invalid-feedback">
#Localizer.General["PhoneNumber is mandatory."]
</div>
</div>
</div>
In the Model the PhoneNumber is filled with the Data from the Database, but the Phonenumber Field doesnt get the Data from the Model. Do i need to declare the Field somewhere? Do i need to change something in the piranha.useredit.js?
I hope somebody can help me.
This is the method in the project Piranha.AspNetCore.Identity that is responsible for saving the user to the database.
https://github.com/PiranhaCMS/piranha.core/blob/master/identity/Piranha.AspNetCore.Identity/Models/UserEditModel.cs#L60
You can use the identity module as a template/ or use NuGet-package Piranha.AspNetCore.Identity and create your own identity Implementation.
Follow this link for more info on Piranha Modules https://piranhacms.org/docs/master/extensions/modules
Related
I have a "Create" view for my "Category" model which should allow the user to select the Warehouse (another model) where the "Category" will be stored (using a dropdown). As I cannot pass 2 viewmodels to a single view, how can I access the Warehouse names inside my Category.Create view?
#model InventoryManSys.Models.Category
#{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Category</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<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="Warehouse" class="control-label"></label>
<select asp-for="Warehouse" class ="form-control" asp-items="ViewBag.Warehouses"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
I heard something about ViewBag being bad practice, is that correct?
You may use sub properties on your view model, or just use viewdata.
Not sure about viewbag. But probably it may help.
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-6.0#pass-data-to-views
Summary of the differences between ViewData and ViewBag
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-6.0#summary-of-the-differences-between-viewdata-and-viewbag
If you can include a new property in your Category view model, consider including a List of warehouse names.
You can use the following code in your view to show warehouse names in a dropdown list:
#Html.DropDownListFor(model => model.SelectedWarehouse, new SelectList(Model.WarehouseNames))
The above code will also assign your choice to a property called SelectedWarehouse in your Category view model.
I use Select List in my projects.
I think the following two links can help you.
Good luck
https://www.c-sharpcorner.com/article/net-core-5-dropdownlist/
https://www.compilemode.com/2021/06/dropdown-list-in-asp-net-core-mvc.html
I have made a Asp.net core view and getting data from database is working, but I want to add adding comments option. Comments send almost properly, but while sending a comment I need to pass "samochodId" from my asp.net core model which was passed first.
<div class="form-group">
<label asp-for="Komentarz.Wiadomosc" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Komentarz.Wiadomosc" class="form-control" />
<span asp-validation-for="Komentarz.Wiadomosc" class="text-danger"></span>
<input asp-for="Komentarz.samochodId" class="form-control" />
</div>
</div>
This works, but I need to remove this line:
<input asp-for="Komentarz.samochodId" class="form-control" /> and do the pass automaticly by getting this Id from My model (Model.Samochod.Id) or maybe from the route?.
How Can I do this?
use hidden field <input type="hidden" asp-for="Komentarz.samochodId" /> and make sure it's within the opening and closing of form <form></form> element
Using ASP .Net Core (Version 2.2, NON-MVC)
I have a database field (Car) which contains text, in HTML format.
I am using an ASP .Net "Input" Helper Tag to display the data (read only) on a webpage.
<input asp-for="Car.Description" asp-format="" disabled class="form-control" />
I would like to display the data as Html.Raw, but am unsure how to do so.
I originally thought I could do something like this (use Html.Raw), but this throws an exception:
<input asp-for=#Html.Raw(Car.Description) asp-format="" disabled class="form-control" />
I then thought the "asp-format" field might have some usable options, but I don't see any.
<div class="form-group row">
<div class="col-2">
<label asp-for="Car.Description"></label>
</div>
<div class="col-10">
<input asp-for="Car.Description" asp-format="" disabled class="form-control" />
</div>
</div>
End result is that I would like to have the display "raw" HTML on the screen.
Thanks
If you want to display the field as read only, you may not even need the "input" helper tag. Just displaying the data in the div should work.
Remove the input helper and just display the text in the div as follows:
<div class="form-group row">
<div class="col-2">
<label asp-for="Car.Description"></label>
</div>
<div class="col-10">
#Html.Raw(Car.Description)
</div>
</div>
Created a custom widget in Sitefinity MVC, with a custom widget designer:
<div class="form-group">
<label for="My Field">My Field</label>
<div class="row">
<div class="col-xs-3">
<input type="text" sf-model="properties.MyField.PropertyValue" class="form-control" id="MyField" />
</div>
</div>
</div>
Defined in both the controller and the model:
public string MyField { get; set; }
When I click edit, the field shows up as expected. I click save, and then hit edit again: value not stored. Does not help to refresh the page, and is not showing up in the widget itself either.
Am I missing a piece? The documentation is a little long-winded and scattered, I've been scouring through it but nothing is jumping out at me.
I see a put request sent in dev tools, but the request payload is empty. Not sure if this is the right request or relevant.
sf-model should be ng-model
<div class="form-group">
<label for="My Field">My Field</label>
<div class="row">
<div class="col-xs-3">
<input type="text" ng-model="properties.MyField.PropertyValue" class="form-control" id="MyField" />
</div>
</div>
</div>
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