<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>asp.net mvc core tips &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/asp-net-mvc-core-tips/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Tue, 30 May 2017 04:49:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>asp.net mvc core tips &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Form Tag Helper in ASP.NET Core MVC</title>
		<link>https://topreviewhostingasp.net/form-tag-helper-asp-net-core-mvc/</link>
					<comments>https://topreviewhostingasp.net/form-tag-helper-asp-net-core-mvc/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Tue, 30 May 2017 04:49:31 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp.net mvc core tips]]></category>
		<category><![CDATA[asp.net mvc core tutorial]]></category>
		<category><![CDATA[mvc form tag helper]]></category>
		<category><![CDATA[mvc tips]]></category>
		<category><![CDATA[mvc tutorials]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=624</guid>

					<description><![CDATA[In this post, I will cover the Form tag helper that is used to bind a Form element to a particular MVC controller action or named route. This tag helper is an alternative to the following HTML helper method syntax: @using (Html.BeginForm("ControllerName", "ActionName")) {    //Form Contents Here } Binding to Controller Action With the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this post, I will cover the Form tag helper that is used to bind a Form element to a particular MVC controller action or named route. This tag helper is an alternative to the following HTML helper method syntax:</p>
<pre class="lang:default decode:true">@using (Html.BeginForm("ControllerName", "ActionName"))
{
   //Form Contents Here
}</pre>
<h2><strong>Binding to Controller Action</strong></h2>
<p>With the form tag helper, you can bind a form to a specific controller action by adding the asp-controller and asp-action tag helper attributes to a form element.</p>
<p>For example, you can bind a form to the Login action of the Account controller as follows:</p>
<pre class="lang:default decode:true">&lt;form asp-controller="Account" 
      asp-action="Login"&gt;
//Your form elements here
&lt;/form&gt;</pre>
<p>Any elements that you add inside the form will be included in the generated HTML. The example above would generate the following HTML:</p>
<pre class="wrap:true lang:default decode:true">&lt;form action="/Account/Login" method="post"&gt;

//Your form elements here

    &lt;input name="__RequestVerificationToken" type="hidden" value="CfDJ8AFtmUdx-b5MkQvAyGYbjFmMGSMv0Fmk7gG4RqGXlkNV6yqKqj6fgqnOh4TLT6ZnWSaqtAbKkgpEB20lvfkc2iOKZKIqt3tJ4Jij8DjmatTrZo-DKVOLwwOzj3kB8VKpFwc0rQMjaJTTC_gVv5f0vAg"&gt;
&lt;/form&gt;</pre>
<p>The form method was defaulted to post. You can specify this manually in your markup if you want. Any HTML attributes you add to the form element will be included in the generated HTML. However, if you attempt to both manually set the action attribute AND the asp-controller / asp-action attributes, the form tag helper will raise an error.</p>
<p>“Cannot override the ‘action’ attribute for &lt;form&gt;. A &lt;form&gt; with a specified ‘action’ must not have attributes starting with ‘asp-route-‘ or an ‘asp-action’ or ‘asp-controller’ attribute.”</p>
<p>The form tag helper also automatically adds an Anti forgery token which will cover in more detail below.</p>
<h2><strong>Adding Parameters</strong></h2>
<p>In some cases, you might need to specify additional parameters for the controller action that you are binding to. You can specify values for these parameters by adding attributes with the asp-route- prefix. For example, it is common for the Login action to have a returnUrl parameter. This can be specified as follows:</p>
<pre class="lang:default decode:true">&lt;form asp-controller="Account" 
      asp-action="Login" 
      asp-route-returnurl="@ViewBag.ReturnUrl" 
      method="post" &gt;
&lt;/form&gt;</pre>
<p>…which would generate a form element with the action attribute containing the specified value for the returnurl parameter:</p>
<pre class="wrap:true scroll:true lang:default decode:true">&lt;form action="/Account/Login?returnurl=%2FHome%2FAbout" method="post"&gt;

//Your form elements here

    &lt;input name="__RequestVerificationToken" type="hidden" value="CfDJ8AFtmUdx-b5MkQvAyGYbjFmMGSMv0Fmk7gG4RqGXlkNV6yqKqj6fgqnOh4TLT6ZnWSaqtAbKkgpEB20lvfkc2iOKZKIqt3tJ4Jij8DjmatTrZo-DKVOLwwOzj3kB8VKpFwc0rQMjaJTTC_gVv5f0vAg"&gt;
&lt;/form&gt;</pre>
<p>You can specify values for as many parameters as you need using attributes with the asp-route- prefix.</p>
<h2><strong>Binding to a Named Route</strong></h2>
<p>Another option is to specify the controller and action using a named route. For example, if I had a route named login defined as follows in my MVC route configuration:</p>
<pre class="lang:default decode:true">routes.MapRoute(
    name: "login",
    template: "login",
    defaults: new { controller = "Account", action = "Login" });</pre>
<p>Then I could bind my form to the login route using the asp-route tag helper attribute:</p>
<pre class="lang:default decode:true">&lt;form asp-route="login" 
      asp-route-returnurl="@ViewBag.ReturnUrl" 
      method="post" &gt;
&lt;/form&gt;</pre>
<p>…which would generate the following HTML:</p>
<pre class="wrap:true scroll:true lang:default decode:true">&lt;form action="/login?returnurl=%2FHome%2FAbout" method="post"&gt;

//Your form elements here

    &lt;input name="__RequestVerificationToken" type="hidden" value="CfDJ8AFtmUdx-b5MkQvAyGYbjFmMGSMv0Fmk7gG4RqGXlkNV6yqKqj6fgqnOh4TLT6ZnWSaqtAbKkgpEB20lvfkc2iOKZKIqt3tJ4Jij8DjmatTrZo-DKVOLwwOzj3kB8VKpFwc0rQMjaJTTC_gVv5f0vAg"&gt;
&lt;/form&gt;</pre>
<h2><strong>Anti-Forgery Token Option</strong></h2>
<p>As we saw earlier, the form tag helper attribute adds a hidden __RequestVerificationToken input. This hidden input contains an anti-forgery token that when used in combination with the [ValidateAntiForgeryToken] attribute on the controller action will help to protect your application against cross-site request forgery. You really should use this on all your forms but if for some reason you need to turn this off for a particular form, you can simply set the asp-anti-forgery token to false.</p>
<pre class="lang:default decode:true  ">&lt;form asp-controller="Account" 
      asp-anti-forgery="false"
      asp-action="Login"&gt;
//Your form elements here
&lt;/form&gt;</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/form-tag-helper-asp-net-core-mvc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
