Search code examples
c#razorasp.net-coreasp.net-core-viewcomponent

Using form in view component ASP.NET Core 1.0


I have view component1:

<form class="form-horizontal">
    <input type="text" name="ip1" class="form-control"/>
    <input type="submit" name="btnSubmit" class="btn btn-default" />
</form>

and view component2:

<form class="form-horizontal">
    <input type="text" name="ip1" class="form-control"/>
    <input type="submit" name="btnSubmit" class="btn btn-default" />
</form>

Two view components is in the same page. But I don't know how to handle post request inside each view component. And how to post a model to a view component? Example code behind or similar:

 public class Component1ViewComponent : ViewComponent
 {
    public Component1ViewComponent()
    {

    }

    public async Task<IViewComponentResult> InvokeAsync(bool isPost)
    {
        if (isPost)
        {
            //handle post request and get model value here
        } else
        {

        }
        return View(model);
    }
}

Solution

  • ViewComponents are not a http request endpoints, so what you are trying to do is not possible. View components come into picture only when a view is being generated.