asp.net-core-viewcomponent

Asp.net mvc compiler error when trying to invoke View Component from view page

让人想犯罪 __ 提交于 2021-02-11 12:03:17
问题 I am trying to create my first View Component in my MVC project but have come to a halt. When I try to invoke my View Component from the razor view page I get an error that states: "The name 'await' does not exist in the current context". Code I use to call the View Component: @await Component.InvokeAsync("Example") View Component class: namespace MyWebApp.ViewComponents { public class ExampleViewComponent : ViewComponent { public IViewComponentResult Invoke() { var user = Account.GetCurrent(

ViewComponents with children

六月ゝ 毕业季﹏ 提交于 2020-05-30 12:59:27
问题 Can I use ViewComponents in a page like this. <vc:parent-component> <some random html> </parent-component> when I try this it renders out only parent component. In cshtml of parent-component I am looking for something like below <h2>Parent component markup</h2> @RenderChildren()?? <h2>Parent component markup end</h2> So result will be <h2>Parent component markup</h2> @RenderChildren() <h2>Parent component markup end</h2> 回答1: You cannot use random html inside ViewComponent tag helper as you

View Component as a Tag Helper does not get Invoked

£可爱£侵袭症+ 提交于 2020-05-26 02:36:21
问题 Invoking a View Component as a Tag Helper was introduced in ASP.NET Core 1.1. (See “Invoking a view component as a Tag Helper”). But the following only returns the Test for VC part of the view. It seems that <vc:annual-orders>…</vc:annual-orders> part does not get invoked at all. Views\Shared\Components\AnnualOrders\Default.cshtml : @{ Layout = ""; } <div>Test for VC</div> <div> <vc:annual-orders> </vc:annual-orders> </div> myProj\ViewComponents\AnnualOrdersViewComponent.cs : public class

How to bind a ModelExpression to a ViewComponent in ASP.NET Core?

自闭症网瘾萝莉.ら 提交于 2020-03-20 06:05:41
问题 I would like to bind a model expression (such as a property) to a view component—much like I would with an HTML helper (e.g., @Html.EditorFor() ) or a tag helper (e.g., <partial for /> )— and reuse this model in the view with nested HTML and/or tag helpers. I am able to define a ModelExpression as a parameter on a view component, and retrieve a lot of useful metadata from it. Beyond this, I start running into roadblocks: How do I relay and bind to the underlying source model to e.g. an asp

Form Post from View Component

醉酒当歌 提交于 2020-02-04 05:04:42
问题 I'm building a small web application to test the new ASP.NET Core MVC. I implemented a View Component that comprises a login form that I need to insert into various places throughout the application. My form currently posts data to a normal Controller that handles the login. Now given that the user provided wrong login information I want to add an error message to the model state and display it under my form, like normal validation. But the thing is since the view component can be integrated

Custom ViewComponent with asp-for as parameter

怎甘沉沦 提交于 2020-01-31 05:03:05
问题 I want wrap this: <textarea asp-for="@Model.Content" ...> into reusable ViewComponent, where property will be parameter: <vc:editor asp-for="@Model.Content" /> I was able to pass asp-for as parameter to the viewcomponent: public class EditorViewComponent : ViewComponent { public IViewComponentResult Invoke(ModelExpression aspFor = null) { //when debugging, aspFor has correct value return View(aspFor); } } But I'm not able to evaluate it in component's view. This does not work: <!--

ViewComponent will not allow manipulation of DOM

本小妞迷上赌 提交于 2020-01-06 05:24:20
问题 In my .net MVVM application I have several view components that are used to render dropdownlists populated from the database. I have discovered that I'm unable to manipulate (re-select) items in the drop down list through jquery after they are rendered. Can anyone explain why this is? View component @using ViewComponents @model XXX.Source @Html.DropDownList(Model.FieldName, new List<SelectListItem>(Model.OptionList), "Choose", new { @multiple = "multiple", @class = "input-sm" } Calling the

Render ViewComponent inside html string

安稳与你 提交于 2020-01-05 05:56:53
问题 Lets say I have a ViewComponent named MyComponent. As of ASP.NET Core 1.1 I can render this ViewComponent by writing this inside a razor view .cshtml page: <vc:my-component></vc:my-component> I want to do something like this: @{string myHtml = "<vc:my-component></vc:my-component>";} @(new HtmlString(myHtml)) I installed and tried RazorEngine by doing this, but this did not work: string template = "<vc:my-component></vc:my-component>"; var result = Engine.Razor.RunCompile(template,

View Component not detecting/returning Ajax request on 2nd submit

一个人想着一个人 提交于 2020-01-05 04:07:12
问题 I've got a really strange problem where my ajax post request works fine the first time after the page has loaded but if I submit my form again without doing a page refresh then the contents are returned to the whole page instead of just updating the DIV. I've spent days on this looking for answers and through trial and error and I suspect the issue relates to Request.Headers["X-Requested-With"] being empty on 2nd submit. Note that I am returning a View Component to the ajax request and my

How to return HTTP Status Codes for errors from Asp.Net Core View Component

梦想与她 提交于 2020-01-03 16:55:53
问题 I am trying to return a HTTP 500 or BadRequest() result from my View Component in Asp.Net Core however this return type does not appear to be available for View Components. Should status code return types be available for View Components or have i got my design wrong? I am calling my controller action via ajax as per below... <a asp-controller="Client" asp-action="LoadVisit" asp-route-id="@item.VisitID" data-ajax="true" data-ajax-method="GET" data-ajax-update="#ClientVisit" data-ajax-mode=