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
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
So I'm relatively new to ASP.NET Razor and I'm getting a consistent error that I'm not sure how to fix. I've created an ASP.NET Core Web Application MVC with Razor Pages.
I'm currently getting the error: InvalidOperationException: Incorrect Content-Type: Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
when I load the project. It's telling me that the error is in the line email = Request.Form["email];
File the error is relating to:
#page
#model IndexModel
#{
ViewData["Title"] = "Login";
var email = "";
var pswd = "";
bool rem = false;
email = Request.Form["email"];
pswd = Request.Form["pswd"];
}
<div class="progress mt-5">
<div class="progress-bar progress-bar-striped progress-bar-animated"
style="width:40%"></div>
</div>
<br />
<div class="container mx-auto bg-warning" style="width:50%;">
<h1 class="title" style="text-align:center">TestWebApp</h1>
<form method="post" class="px-3 py-5">
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text bg-primary border-0 text-light" style="width:100px">Email</span>
</div>
<input type="email" class="form-control" id="email" placeholder="Enter email" name="email" value="">
</div>
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text bg-primary border-0 text-light" style="width:100px">Password</span>
</div>
<input type="password" class="form-control" placeholder="Enter password" name="pswd" value="#pswd">
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember" value="#rem"> Remember me
</label>
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
</div>
The weird thing is I ran this code multiple times before, just making small design changes with no relation to the form itself and it worked fine without an error but has now started showing this error and I'm not sure how to fix it. Would be very thankful for some help.
I don't know what does Request stand for in your code. Because nothing in your page is named that way. If you have some sort of shortcut somewhere I guess you're getting the Request property from your model. So you're referring to the PageModel's Request property that gets exposed because your model IndexModel inherits from PageModel.
If the aforementioned is true, then I suspect you're using the Request.Form in the wrong place here.
What are you trying to accomplish reading Request.From at that point in time, i.e - when the razor engine parses and renders your template? Please note, there's no form there yet. So that's probably why you're getting such error.
The error could be misleading because your code is messing with the request too early in the process. You're basically forcing the engine to read the form when nothing is there and the exception that pops out is telling you that the form doesn't have, neither application/x-www-form-urlencoded nor multipart/form-data as its Content-Type header value. But again, the main reason for the failure is that you're using the Request.Form in the wrong place.
Perhaps you should explain better what your final goal is, so we can get a better idea of what can be wrong?
Hope this helps!
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>
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.