ASP.NET - Read Component Parameter In Razor View

Viewed 54

I have an existing ASP.NET app that uses one Razor component throughout. Unfortunately, this component does not have a model associated with it. I'm in a scenario where I need to add one parameter. At this time, I have the following in the component host view:

@await Component.InvokeAsync("MyTextField", new { Align = "Left" })

The component Razor code currently looks like this:

MyTextField.cshtml

<div class="text-right">
  <form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
    <input id="inputField" class="input-text" type="text" />
    <button class="submit-button" type="submit">Submit</button>
  </form>
</div>

Currently, the component renders. However, I want to get the value of the Align parameter, if it exists, in the MyTextField.cshtml view. Is there a way for me to get a parameter value there? If so, how?

Thanks

1 Answers

You can do that in this way:

@await Component.InvokeAsync("MyTextField", new User{ Align = "Left" })

and in View (cshtml file):

@using UserNamespace
@model User

<div class="text-right">
  <form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
    <input id="inputField" class="input-text" type="text" value="@Model.Align" />
    <button class="submit-button" type="submit">Submit</button>
  </form>
</div>

Related