Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7578099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:21:03+00:00 2026-05-30T17:21:03+00:00

I need a form on my ASP.NET MVC Razor page. My preference would be

  • 0

I need a form on my ASP.NET MVC Razor page. My preference would be to use the following syntax:

@using (Html.BeginForm())
{
}

However, I need several attributes added to the form. So I ended up with something like the following:

@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "value" }))
{
}

However, this has an undesired side effect. If there are query arguments in this page’s request, the first form passes them along when the form is submitted. However, the second version does not.

I really don’t know why BeginForm() doesn’t support attributes, but is there a straight-forward way to add attributes to BeginForm() and still pass along any query arguments when the for is submitted?

EDIT:

After looking into this, it would seem the best solution is something like this:

<form action="@Request.RawUrl" method="post" name="value">
</form>

However, when using this syntax, client-side validation is disabled. It seems there is no good solution to this situation without more complicated and potentially unreliable constructs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:21:05+00:00Added an answer on May 30, 2026 at 5:21 pm

    That’s indeed true, but I would go with a custom helper in order to preserve the form context inside which is used for client side validation:

    public static class FormExtensions
    {
        private static object _lastFormNumKey = new object();
    
        public static IDisposable BeginForm(this HtmlHelper htmlHelper, object htmlAttributes)
        {
            string rawUrl = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
            return htmlHelper.FormHelper(rawUrl, FormMethod.Post, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    
        private static int IncrementFormCount(IDictionary items)
        {
            object obj2 = items[_lastFormNumKey];
            int num = (obj2 != null) ? (((int)obj2) + 1) : 0;
            items[_lastFormNumKey] = num;
            return num;
        }
    
        private static string DefaultFormIdGenerator(this HtmlHelper htmlhelper)
        {
            int num = IncrementFormCount(htmlhelper.ViewContext.HttpContext.Items);
            return string.Format(CultureInfo.InvariantCulture, "form{0}", new object[] { num });
        }
    
        private static IDisposable FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes)
        {
            var builder = new TagBuilder("form");
            builder.MergeAttributes<string, object>(htmlAttributes);
            builder.MergeAttribute("action", formAction);
            builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
            bool flag = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
            if (flag)
            {
                builder.GenerateId(htmlHelper.DefaultFormIdGenerator());
            }
            htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
            var form = new MvcForm(htmlHelper.ViewContext);
            if (flag)
            {
                htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"];
            }
            return form;
        }
    }
    

    which could be used like this:

    @using (Html.BeginForm(htmlAttributes: new { name = "value" }))
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET MVC 3, I've declared an ajax form like this: @using (Ajax.BeginForm(SaveRegistrationConfirmationRequest, null,
We are using ASP.Net MVC TempData to store form data between page refreshes. We
I'm using ASP.NET MVC. So I have a form on my page: <form id=MyForm
Im using regular asp.net c# (webforms) i need to build a simple form that
How do you set the id of an Html.Form in ASP.NET MVC 2? I
I'm using asp.NET MVC 2 for my current project and I need to validate
I am using ASP.Net MVC. I have a partial view which has a form
I need to implement a regular expression in my asp.net mvc(C#) application using jquery.
I have an ASP.Net MVC page where I want to use AJAX to update
I have an asp.net mvc application and i need to to use ajax in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.