Can I introduce an MVC6 view component?

Is there any way to represent the form inside the view component? I know this is not possible for partial views. The View component has its own controller, so the goal of this question is that I can use this controller to submit the form displayed by the component view.

It is not mentioned in MVC6 considers components of docs . So this is not possible, I think, but maybe I'm wrong :)

+5
source share
1 answer

Like partial views, view components can also display HTML. Since the display provided by your view component has a valid form tag in it, yes, you can submit forms.

@model YourNamespace.LoginVM <form asp-controller="Home" asp-action="Login"> <input asp-for="Name" /> <input asp-for="Password" /> <input type="submit" /> </form> 

You need to make sure that you have a HomeController login action method

 [HttpPost] public ActionResult Login(LoginVM model) { //do something :) } 

It is important to remember that you should not name this component of the view inside another form tag. General rule: you should not embed forms .

 <form id="main-form" asp-controller="main" asp-action="submit" method="post"> <input asp-for="LocationName" /> <input type="submit" /> </form> @Component.Invoke("Login") 
+5
source

Source: https://habr.com/ru/post/1237390/


All Articles