Error CS0411 ASP.NET Core Web Application - c#

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.

Related

Using foreach in razor pages with hidden asp-for inputs result in found elements with non-unique id in browser console

I have two partials. One is _ProductList.cshtml:
#model IList<ProductDto>
#foreach (var product in Model) {
<div class="col-md-4 col-sm-6 px-2 mb-4">
<partial name="_SingleProduct" for="#product"/>
</div>
}
and _SingleProduct.cshtml:
#model ProductDto
<form method="post" asp-page="/Basket/Index" class="text-center">
<div class="card-body card-body-hidden">
<h4 class="fs-sm">#Model.AtlantisCode</h4>
#if (Model.Availability) {
<button class="btn btn-primary btn-sm d-block w-100 mb-2" type="submit">
<i class="ci-cart fs-sm me-1"></i>Προσθήκη στο καλάθι
</button>
}
else {
<button class="btn btn-secondary btn-sm d-block w-100 mb-2" type="submit" asp-page="/Contact/Index" asp-page-handler="ItemInfo">
<i class="ci-mail fs-sm me-1"></i> Ρωτήστε μας
</button>
}
<input type="hidden" asp-for="#Model.Id" name="id" />
<input type="hidden" asp-for="#Model.DisplayPrice" name="displayPrice"/>
<input type="hidden" asp-for="#Model.OfferPrice" name="offerPrice"/>
<input type="hidden" asp-for="#Model.IsOffer" name="isOffer"/>
</div>
</form>
So in a page with 12 products list it results in a browser warnings for each hidden input like:
Found 12 elements with non-unique id #Products_Results_product_DisplayPrice
which actually makes sense since there are 12 elements with same ids.
Some time I used https://github.com/dotnet-architecture/eShopOnWeb to check some of best practices and use in my project. So thing is that in eShopOnWeb project there is also a foreach with a partial with same technique. And there are no warnings at all. There are no custom id procedure. Even rendered page html have same ids in hidden inputs. But no warning.
Anyone please can tell why? What am i missing?
Thanks everyone and sorry for long post.
try this
<input type="hidden" asp-for="#Model.Id" name="id#Model.Id" />
....and so on
I only don' t know why you need the name at all since you have already asp-for="#Model.Id"
I would prefer
<input type="hidden" asp-for="Id" value="#Model.Id"/>
.... and so on

ASP.NET Razor Pages Error: InvalidOperationException: Incorrect Content-Type

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!

No data binding on form submission

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

Bootstrap Button_Click Event Not Firing

I have a form designed with a button which should call a stored procedure. For now, I just want the button to display text in a textbox (just to make sure the button is working). However, I cannot get it to fire the button_click event.
Here is what I have:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="container-fluid">
<section class="container">
<div class="container-page">
<div class="col-md-6">
<h3 class="dark-grey">Registration</h3>
<div class="form-group col-lg-12">
<label>Username</label>
<input type="" name="" class="form-control" id="" value="">
</div>
<div class="form-group col-lg-6">
<label>Password</label>
<input type="password" name="" class="form-control" id="" value="">
</div>
<div class="form-group col-lg-6">
<label>Repeat Password</label>
<input type="password" name="" class="form-control" id="" value="">
</div>
<div class="form-group col-lg-6">
<label>Email Address</label>
<input type="" name="" class="form-control" id="" value="">
</div>
<div class="form-group col-lg-6">
<label>Repeat Email Address</label>
<input type="" name="" class="form-control" id="" value="">
</div>
<div class="col-md-6">
<h3 class="dark-grey">Terms and Conditions</h3>
<p>
By clicking on "Register" you agree to the Terms and Conditions.
</p>
<button type="submit" class="btn btn-primary" onclick="btnSumbit_Click">Register</button>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</div>
</section>
</div>
</form>
</body>
</html>
My c# code is:
protected void btnSumbit_Click(object sender, EventArgs e)
{
TextBox1.Text = "Registered";
}
Any idea why this will not work?
My guess is that you are getting a normal <button> mixed up with an <asp:Button>.
Your current implementation wants to run a javascript function name btnSubmit_Click and does not have any connection to your C# code-behind.
...
<asp:Button type="submit" CssClass="btn btn-primary" OnClick="btnSumbit_Click" runat="server">Register</asp:Button>
...
I haven't been able to test if it works myself.
If you need to use an html button, instead of an ASP Button control (for instance, if you need something other than text in the button), you can still do it like this (using runat=server and OnServerClick):
<button runat="server" type="button" id="updateButton" onserverclick="UpdateEmail_Click" class="btn btn-success btn-block" title="<%$ Resources:UserMessages, UpdateCaps %>"><asp:Localize ID="Localize5" runat="server" Text="<%$ Resources:UserMessages, UpdateCaps %>"></asp:Localize></button>
If you're using a standard HTML button, you need to:
Add runat="server" to your button.
Set your button to use onserverclick instead of onclick.
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlbutton.onserverclick(v=vs.110).aspx
So:
<button runat="server" class="btn btn-primary" onserverclick="Method_Name"...
Alternatively, you can use Bootstrap classes with the <asp:Button... tags as well. You'll simply apply them to an attribute called CssClass instead of `class'.
So:
<asp:Button type="submit" runat="server" CssClass="btn btn-primary" OnClick="Method_Name"...

Jasny Bootstrap File Upload Control

I am using Jasny Bootstrap file upload control in my asp.net project.
It looks really good, but im unable to send the attached file to the server (a .ASHX webservice)
My layout code look like this
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input span3"><i class="icon-file fileupload-exists"></i> <span class="fileupload-preview"></span></div><span class="btn btn-file"><span class="fileupload-new">Select file</span><span class="fileupload-exists">Change</span><input type="file" /></span>Remove
</div>
</div>
The HTML contains an <input type="file" />. You need to set the name attribute of the input element. Also make sure that you're using enctype="multipart/form-data" in the form.
<form action="myscript.ashx" method="post" enctype="multipart/form-data">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="input-append">
<div class="uneditable-input span3"><i class="icon-file fileupload-exists"></i> <span class="fileupload-preview"></span></div>
<span class="btn btn-file"><span class="fileupload-new">Select file</span><span class="fileupload-exists">Change</span><input type="file" name="myupload"/></span>
Remove
</div>
</div>
</form>

Categories