I am trying to use a Razor argument and pass it into Blazor for further processing, but I get this error message "Component attributes do not support complex content (mixed C# and markup)" on the #onclick event I am trying to build on the img tag below:
<tr>
#{
for (int j = 0; j < Candidates.Length; j++)
{
<th>
<div class="grow">
<img src="/Candidates/#(Candidates[j].ToString()).jfif" alt="#Candidates[j].ToString()" #onclick="IncrementScore(#j)" />
</div>
</th>
}
}
</tr>
Any suggestions would be greatly appreciated!
The main issue with your code because of which you've got a compiler error is the way you call the IncrementScore method. You should realize that #onclick is not an Html attribute to which you should assign a value, in this case a method that gets a value.
The #onclick attribute is a compiler directive, instructing the compiler how to form an event handler that should be invoked when the element is clicked, the target of the event, etc. In your case, you wish to call a method and pass it a value. This can only be done by using a lambada expression as follows:
#onclick="#(()=> IncrementScore(<value to pass to the method>))"
The following code snippet illustrates how to call the IncrementScore method properly when you're using a for loop or foreach loop. The distinction is very important regarding local variables that are passed to methods in loops
You can put the following code in the Index component, and run it as is:
#*#for (int j = 0; j < Candidates.Count; j++)
{
int localVariable = j;
<img src="#Candidates[j].Src" #onclick="#(()=>
IncrementScore(localVariable))" />
}*#
#foreach (var candidate in Candidates)
{
Random random = new Random();
<img src="#candidate.Src" #onclick="#(()=>
IncrementScore(random.Next(0,9)))" />
}
<p>#Scores.ToString()</p>
#code {
List<Candidate> Candidates = Enumerable.Range(1, 10).Select(i => new
Candidate { CandidateName = i }).ToList();
private int Scores;
private Task IncrementScore(int score)
{
Scores = score;
return Task.CompletedTask;
}
public class Candidate
{
public int CandidateName { get; set; }
public string Src => $"{CandidateName}.jfif";
}
}
Hope this helps...
You may also encapsulate the complex content in a code method:
<NavLink href="#GetLink()" class="button is-link">
Details
</NavLink>
#code {
[Parameter]
public string? Id { get; set; }
private string GetLink()
{
return this.Id != null ? $"/details/{this.Id}" : "#";
}
}
I came to this page in search for a solution for the same error:
Component attributes do not support complex content (mixed C# and markup)
In my case was all about the href link construction. I followed your solution on the accepted answer comment and it worked. Thanks #Øyvind Vik
Just in case it help others i'll leave my code as an example.
foreach (List<string[]> lss in searchResults)
{
var link = $"genericform/{entidadeSelecionada}/{lss.FirstOrDefault()[1]}";
<tr>
#foreach (string[] st in lss)
{
if (camposAListar.Contains(st[0].ToLower()))
{
<td>#st[1]</td>
}
}
<td>
<NavLink href="#link">
<span class="oi oi-list-rich" aria-hidden="true"></span> Utilizador
</NavLink>
</td>
</tr>
}
Related
Trying to hack a transition experience when removing a row from a column. Something like Vue/React. Calling InvokeVoidAsync to append a class that will perform an animation. The javascript call also has a sleep call that will postpone the actual item from being removed on the server side. After the server removes the item, the class is applied to the item below it. Example of a table row appending the class to the next table row UI Side
I created a Blazor Server Side Application and updated the Index.razor page, added a couple js functions, and a css file.
Index.razor
#page "/"
<h2> #Pizzas.Count() Pizza's</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var pizza in Pizzas)
{
<tr id="#(pizza.ID)">
<td>#pizza.Name</td>
<td><button #onclick="#(async _ => await RemovePizza(pizza))">Remove</button></td>
</tr>
}
</tbody>
</table>
#inject IJSRuntime JSRuntime
#code {
public record Pizza(Guid ID, string Name);
public string[] PizzaTypes => new[] { "Pepperoni", "Cheese", "Hawaiian", "Veggie", "Bacon", "Southwestern BBQ", "Cheeseburger", "Buffalo", "Meat", "Supreme" };
public List<Pizza> Pizzas { get; set; } = new();
protected override void OnInitialized()
{
base.OnInitialized();
try
{
for (int i = 0; i < 10; i++)
AddPizza();
}
catch (Exception)
{
throw;
}
StateHasChanged();
}
public void AddPizza()
{
Pizzas.Add(new Pizza(Guid.NewGuid(), PizzaTypes[new Random().Next(0, PizzaTypes.Length - 1)]));
}
public async Task RemovePizza(Pizza pizza)
{
await JSRuntime.InvokeVoidAsync("animateTransition", pizza.ID, "fade-away", 1000);
Pizzas.Remove(pizza);
}
}
JS Functions
var sleep = async (ms) => new Promise(resolve => setTimeout(resolve, ms));
var animateTransition = async (id, className, duration) => {
const el = document.getElementById(id);
el.classList.add(className);
await sleep(duration);
};
CSS
#keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
tr.fade-away {
animation-name: fadeOut;
animation-duration: 1s;
}
Turns out I was missing the #key attribution within the loop. Such a novice mistake.
https://blazor-university.com/components/render-trees/optimising-using-key/
Updated Code
#foreach (var pizza in Pizzas)
{
<tr #key="pizza" id="#(pizza.ID)">
<td>#pizza.Name</td>
<td><button #onclick="#(async _ => await RemovePizza(pizza))">Remove</button></td>
</tr>
}
I'm new to Blazor and .NET 6, but I am working on a small app. I have LiteDB storing data on the backend, and my Razor component is fetching it. The problem is that, while 80% of the time it works as expected, the other 20% of the time the data gets populated as null in the Razor component before the database call has even processed. When debugging this, I see the code get to the database call in the backend, then it jumps back to the frontend Razor component which now has a null object, and then it jumps back to the backend where the database call has now been completed. The page populates from the null object though, so the data is not there. I get an error in the console, but it's just stating that the object was not set to an instance of an object at a foreach loop in my code. Here's my code:
#page "/origin"
#page "/origin/{id:int}"
#inject HttpClient http
#if (id == null)
{
<PageTitle>Origin Not Found</PageTitle>
<h3>Origin Not Found</h3>
}
else
{
<PageTitle>Origin: #origin.Name</PageTitle>
<h3>Origin: #origin.Name</h3>
<table class="table">
<tbody>
<tr>
<td>Description: </td>
<td>#origin.Description</td>
</tr>
<tr>
<td>Starting Health: </td>
<td>#origin.StartingHealth</td>
</tr>
<tr>
<td>Ground Movement Speed: </td>
<td>#origin.GroundMovementSpeed</td>
</tr>
#foreach(var stat in #essences)
{
<tr>
<td>Essence Increase Option: </td>
<td>#stat</td>
</tr>
}
<tr>
<td>Origin Benefit: </td>
<td>#origin.OriginBenefit</td>
</tr>
</tbody>
</table>
}
#code {
[Parameter]
public int id { get; set; }
private Origin origin = new();
private List<Essence> essences = new();
protected async override Task OnInitializedAsync()
{
var result = await http.GetFromJsonAsync<Origin>($"/api/origin/{id}");
if(result != null)
{
origin = result;
essences = result.EssenceIncreaseOptions;
}
}
}
Now if I remove the foreach loop (and the essence information I want there), then the code works 100% of the time. So there's something about that foreach loop that's causing it to fail 20% of the time, but I'm just not sure what. Any help would be appreciated. Thanks in advance.
In the posted code, the only point in the foreach(var stat ...) that could yield an NRE is <td>#stat</td>
So your Essence class probably has an override ToString() that causes the null de-reference.
What you can do is put a conditional check before the #foreach loop and when essences is null show some message saying loading etc, also, put StateHasChanged() after completion of fetching of data.
#if(essences.Count>0)
{
#foreach(var stat in #essences)
{
<tr>
<td>Essence Increase Option: </td>
<td>#stat</td>
</tr>
}
}
else {loading data}
and
protected async override Task OnInitializedAsync()
{
var result = await http.GetFromJsonAsync<Origin>($"/api/origin/{id}");
if(result != null)
{
origin = result;
essences = result.EssenceIncreaseOptions;
StateHasChanged();
}
}
I am attempting to test our Blazor project with bUnit and am having issues with a bUnit Assert.
This is the component I am trying to test:
<div>
foreach(KeyValuePair<String, String> entry in Dictionary<String, String>)
{
<div class="form-check">
<input class="form-check-input" type="radio" name="myAwesomeName-#ChosenDocument.docId" id="myAwesomeName#ChosenDocument.docId-#entry.Key" checked=#(firstOptionEnabled) #onclick="() => doSomethingAwesome = entry.Key">
<label class="form-check-label" for="myAwesomeName-#ChosenDocument.docId-#entry.Key"> #entry.Value </label>
</div>
firstOptionEnabled = false;
}
</div>
#code
{
[Parameter]
public Document ChosenDocument { get; set; }
}
The test I have written looks like this:
[Fact]
public async void MyTest()
{
var cut = RenderComponent<MyComponent ChosenDocument="documentToTest">();
IRefreshableElementCollection<IElement> allElements = cut.FindAll("div [class=\"form-check\"]");
// These are the <div class="form-check"> elements
foreach(IElement element in allElements)
{
// These are the <input> elements
foreach(INode node in element.ChildNodes)
{
// Here, I want to assert on data within each of these `INode`
}
}
}
My problem is happening with the Assert I want to perform.
I want to assert the id or name of each of the input elements like so:
Assert.Equal(("myAwesomeName-" + ChosenDocument.docId), <something here ...>);
Where <something here ...> is either id or name of the input element. However, I cannot seem to get hold of the id or name from the component under test.
When I debug the test and look in the "Locals" window, I can see this:
- node {AngleSharp.Html.Dom.HtmlInputElement} AngleSharp.Dom.INode {AngleSharp.Html.Dom.HtmlInputElement}
// ...
Id "myAwesomeName-1234567890-text" string
// ...
Name "myAwesomeName-1234567890" string
// ...
So, it looks like those two pieces of information should be available to me within my Assert, but they are not; well, at least not evident to me.
How can I access those two pieces of data I need for my Assertion?
Does something like this work? You should be able to grab all the input elements directly:
var inputs = cut.FindAll(".form-check-input");
foreach (var input in inputs)
{
Assert.Equal($"myAwesomeName-{ChosenDocument.docId}", input.GetAttribute("name"));
}
or even more concise:
var inputs = cut.FindAll(".form-check-input");
Assert.All(inputs, i => Assert.Equal($"myAwesomeName-{ChosenDocument.docId}", i.GetAttribute("name")));
This is now fixed. A combination of Ish's suggestion below plus adding calls to #HiddenFor in the view resolved the problem.
I have an ASP.NET MVC 5 web application where users can mark a defect as resolved. I want to display a list of potentially related defects, with check-boxes that users can tick to indicate that yes, this is the same defect, and should also be marked as resolved.
So I have a View Model with a property that is a collection, each member of which contains a defect object property and Boolean IsSameDefect property. This all works fine in the GET action method and in the view. I can display the related defects and tick the boxes.
The problem arises in the POST action when I want to update the data. At this point the property (the collection of potentially related defects) is null. I'm having a hard time trying to figure out how to pass this data back to the controller?
Code as requested ...
// GET: /DefectResolution/Create
public ActionResult Create(int ciid)
{
int companyId = User.CompanyID();
DefectResolutionCreateViewModel drcvm = new DefectResolutionCreateViewModel(ciid, companyId);
return View(drcvm);
}
// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
DefectResolutions currentResolution = drcvm.DefectResolution;
currentResolution.CreatedOn = System.DateTime.Now;
currentResolution.UserID = User.UserID();
if (ModelState.IsValid)
{
unitOfWork.DefectResolutionRepository.Insert(currentResolution);
if (currentResolution.ResolutionStatusID == 2)
{
//code breaks here as drcvm.RelatedUnresolvedDefects is null
foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
{
if (relatedDefect.IsSameDefect)
{
DefectResolutions relatedResolution = new DefectResolutions();
relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
relatedResolution.CreatedOn = System.DateTime.Now;
relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
relatedResolution.UserID = User.UserID();
}
}
}
unitOfWork.Save();
return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
}
return View(drcvm);
}
In the view ...
#model Blah.ViewModels.DefectResolution.DefectResolutionCreateViewModel
#{
ViewBag.Title = "Create Defect Resolution";
var relatedDefects = Model.RelatedUnresolvedDefects;
}
... and later in the view ...
#for (int i = 0; i < relatedDefects.Count(); i++ )
{
<tr>
<td>
#Html.EditorFor(x => relatedDefects[i].IsSameDefect)
</td>
</tr>
}
I followed Ish's suggestion below, and modified the code to refer to Model.RelatedUnresolvedDefects directly instead of using a variable as I had been doing. This does get me a bit further. The view model's RelatedUnresolvedDefects property is no longer null. But only RelatedUnresolvedDefects.IsSameDefect has a value. RelatedUnresolvedDefects.RelatedChecklist is null. Here's the controller code again showing where it now breaks ...
// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
DefectResolutions currentResolution = drcvm.DefectResolution;
currentResolution.CreatedOn = System.DateTime.Now;
currentResolution.UserID = User.UserID();
if (ModelState.IsValid)
{
unitOfWork.DefectResolutionRepository.Insert(currentResolution);
if (currentResolution.ResolutionStatusID == 2)
{
//prior to change, code used to break here
foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
{
if (relatedDefect.IsSameDefect)
{
DefectResolutions relatedResolution = new DefectResolutions();
//code now breaks here because relatedDefect.RelatedChecklist is null
relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
relatedResolution.CreatedOn = System.DateTime.Now;
relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
relatedResolution.UserID = User.UserID();
}
}
}
unitOfWork.Save();
return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
}
return View(drcvm);
}
Without knowing your code.I suggest you to use for loop instead of foreach while rendering the defects in View (.cshtml).
Editing Answer based on your code.
Following statement in the view creating problem
var relatedDefects = Model.RelatedUnresolvedDefects;
You should directly iterate over the Model.RelatedUnresolvedDefects property in the loop.
#for (int i = 0; i < Model.RelatedUnresolvedDefects.Count(); i++ )
{
<tr>
<td>
#Html.EditorFor(x => Model.RelatedUnresolvedDefects[i].IsSameDefect)
</td>
</tr>
}
I'm using MVC 4 with Razor Syntax to create a collection based on a class that was created using scaffolding (Database first based development) and I can add the first collection to the Session and return it to the Index view and display it on the page.
When I attempt to add a second collection to the Session Variable it gives me a error.
Unable to cast object of type
'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type
'EagleEye.Models.tblTask'.
What am I doing wrong - how do I add 2 collections to the session?!
Index.cshtml (My Index view using Razor syntax)
#model List<myApp.Models.tblTask>
<table>
#{
foreach (var tblTask in Model)
{
<tr>
<td>
TaskName: #tblTask.Name
</td>
<td>
Desc: #tblTask.Description
</td>
<td>
Schedule: #tblTask.Freq #tblTask.FreqUnit
</td>
<td>
Reocurring?: #tblTask.ReocurringTask.ToString()
</td>
</tr>
}
}
</table>
Here's the "ActionResult" portion of the code from my HomeController.cs:
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
TaskCollection.Add((tblTask)Session["TaskCollection"]);
}
TaskCollection.Add(newTask);
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}
public ActionResult Index()
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
TaskCollection = (List<tblTask>)Session["TaskCollection"];
}
return View(TaskCollection);
}
When I add the first entry it works fine and shows up on my index view. When I try to add the second collection of tasks, it tells me:
Unable to cast object of type
'System.Collections.Generic.List`1[EagleEye.Models.tblTask]' to type
'EagleEye.Models.tblTask'.
I've been fighting this for a few days now and have been developing for a while, but am just beginning to learn the power of asking questions when I'm stumped (instead of just continuing to beat my head against the wall until something caves in (often my head), so if my question is not well formed, please let me know.
Thanks!
Dan
Because, inside your if condition, you are casting the Session["TaskCollection"](which is a collection of tblTask to a single instance of tblTask.
This should work.
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
//Check whether the collection exist in session, If yes read it
// & cast it to the tblTask collection & set it to the TaskCollection variable
if (Session["TaskCollection"] != null)
{
TaskCollection= (List<tblTask>) Session["TaskCollection"];
}
if(newTask!=null)
TaskCollection.Add(newTask);
//Set the updated collection back to the session
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}
I finally see the light -- Note the change in the HomeController.cs "TaskCollection = (List)Session["TaskCollection"]; "
[HttpPost]
public ActionResult CreateTask(tblTask newTask)
{
var TaskCollection = new List<tblTask>();
if (Session["TaskCollection"] != null)
{
//Here is the line that changed -- the following line works~
TaskCollection = (List<tblTask>)Session["TaskCollection"];
}
TaskCollection.Add(newTask);
Session["TaskCollection"] = TaskCollection;
return RedirectToAction("Index");
}